Pickling new-style classes w/ getinitargs

D

Dave Brueck

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))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top