栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用“腌”

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用“腌”

你想做什么?这个对我有用:

class A(object):    def __init__(self):        self.val = 100    def __str__(self):        """What a looks like if your print it"""        return 'A:'+str(self.val)import picklea = A()a_pickled = pickle.dumps(a)a.val = 200a2 = pickle.loads(a_pickled)print 'the original a'print aprint # newlineprint 'a2 - a clone of a before we changed the value'print a2printprint 'Why are you trying to use __setstate__, not __init__?'print

所以这将打印:

the original aA:200a2 - a clone of a before we changed the valueA:100

如果需要setstate:

class B(object):    def __init__(self):        print 'Perhaps __init__ must not happen twice?'        print        self.val = 100    def __str__(self):        """What a looks like if your print it"""        return 'B:'+str(self.val)    def __getstate__(self):        return self.val    def __setstate__(self,val):        self.val = valb = B()b_pickled = pickle.dumps(b)b.val = 200b2 = pickle.loads(b_pickled)print 'the original b'print bprint # newlineprint 'b2 - b clone of b before we changed the value'print b2

打印:

Why are you trying to use __setstate__, not __init__?Perhaps __init__ must not happen twice?the original bB:200b2 - b clone of b before we changed the valueB:100


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/669835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号