zoukankan      html  css  js  c++  java
  • Simple example of use of __setstate__ and __getstate__

    class Foo(object):
        def __init__(self, val=2):
            self.val = val
        def __getstate__(self):
            print ("I'm being picked")
            self.val *= 2
            return self.__dict__
    
    
        def __setstate__(self, d):
            print("I'm being unpickled with these values:", d)
            self.__dict__ = d
            self.val *= 3
    
    import pickle
    f = Foo()
    f_string = pickle.dumps(f) # 带s的,可以理解为string
    print(f_string)
    
    f_new = pickle.loads(f_string)
    print(f_new)


    I'm being picked
    b'x80x03c__main__ Foo qx00)x81qx01}qx02Xx03x00x00x00valqx03Kx04sb.'
    I'm being unpickled with these values: {'val': 4}
    <__main__.Foo object at 0x0000028927245B70>

    新战场:https://blog.csdn.net/Stephen___Qin
  • 相关阅读:
    第一次作业
    第五次作业
    第四次作业
    第三次作业
    第二次作业
    第一次作业
    第五次作业
    第四次作业
    第三次作业
    第二次作业
  • 原文地址:https://www.cnblogs.com/Stephen-Qin/p/11251116.html
Copyright © 2011-2022 走看看