开发者

what does '__getnewargs__' do in this code

开发者 https://www.devze.com 2022-12-15 14:57 出处:网络
class NavigableString(unicode, PageElement): def __new__(cls, value): if isinstance(value, unicode): return unicode.__new__(cls开发者_运维技巧, value)
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),)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消