Pickling new-style classes w/ getinitargs

Dave Brueck dave at pythonapocrypha.com
Tue Feb 10 17:01:29 EST 2004


What is the correct way to pickle a new-style class that makes use of
__getinitargs__? In the test below, __getinitargs__ gets called only on
old-style classes (pickle appears to call __getinitargs__ only if the object
type is InstanceType, but new-style class instances have a type of the the
class). Google didn't turn up anything obvious; I'm starting my search on
SourceForge but it's extremely slow at the moment.

Thanks for any pointers,
Dave


The test:

from pickle import dumps

class A(object):
    def __init__(self, i):
        self.i = i

    def __getinitargs__(self):
        print 'newstyle getinitargs!'
        return (self.i,)

class B:
    def __init__(self, i):
        self.i = i

    def __getinitargs__(self):
        print 'oldstyle getinitargs!'
        return (self.i,)

dumps(A(1))
dumps(B(2))





More information about the Python-list mailing list