开发者

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),)


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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜