What does '__getnewargs__' do in this code

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),) 
+7
python
source share
1 answer

Try the following:

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

That is: __getnewargs__ tells pickle.dumps sort x so that pickle.loads back from this line will use NavigableString.__new__ with the correct argument.

+10
source share

All Articles