Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does '__getnewargs__' do in this code

Tags:

python

class NavigableString(unicode, PageElement):

    def __new__(cls, value):
        if isinstance(value, unicode):
            return unicode.__new__(cls, value)
        return unicode.__new__(cls, value, DEFAULT_OUTPUT_ENCODING)

    def __getnewargs__(self):#this line
        return (NavigableString.__str__(self),)
like image 270
zjm1126 Avatar asked Dec 30 '09 03:12

zjm1126


1 Answers

Try this:

x = NavigableString('foop')
y = pickle.dumps(x)
z = pickle.loads(y)
print x, z

I.e.: __getnewargs__ tells pickle.dumps to pickle x in such a way that a pickle.loads back from that string will use NavigableString.__new__ with the proper argument.

like image 107
Alex Martelli Avatar answered Sep 27 '22 22:09

Alex Martelli