2009-12-30 71 views
7
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),) 

回答

10

試試這個:

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

即:__getnewargs__告訴pickle.dumps鹹菜x以這樣的方式,一個pickle.loads從該字符串後面會使用NavigableString.__new__用正確的論據。