Pickle question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gonçalo Rodrigues

    Pickle question

    Hi,

    Can I force the pickling/unpickling mechanism to call the class
    constructor? I thought __getinitargs__ would do the job, but the
    following shows it does not.
    [color=blue][color=green][color=darkred]
    >>> class Test(object):[/color][/color][/color]
    def __init__(self, **kwargs):
    super(Test, self).__init__( **kwargs)
    self.__dict__.u pdate(kwargs)
    def __getinitargs__ (self):
    print "Calling __getinitargs__ ."
    return ()
    def __getattr__(sel f, attrib):
    print "Getting %s." % attrib
    raise AttributeError

    [color=blue][color=green][color=darkred]
    >>> a = Test(name = None)
    >>> s = pickle.dumps(a)[/color][/color][/color]
    Getting __getstate__.
    Getting __slots__.[color=blue][color=green][color=darkred]
    >>> b = pickle.loads(s)[/color][/color][/color]
    Getting __setstate__.[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I thought unpickling with __getinitargs__ present would go as:

    1. Call class constructor with args read from pickle and return inst.
    2. Update inst by either calling __setstate__ (if there is) or just
    update inst.__dict__.

    What am I missing?

    With my best regards,
    G. Rodrigues
Working...