Prev: need help: Is there is a way to get someone's calendar from mail exchange server with python
Next: Extending objects by a method?
From: Chris Rebert on 15 Jul 2010 07:12 On Thu, Jul 15, 2010 at 3:34 AM, Alan <alanwilter(a)gmail.com> wrote: > Hi there, > This is more an exercise to myself to understand python3. I took a code I > wrote (acpype) and I am trying to make it compatible with either python 3 or > 2. > I am trying to make a pickle file compatible with either python 3 and 2 as I > believe it should be possible. > I've looked at http://docs.python.org/py3k/library/pickle.html but still, > when I create a pickle file for an object with python 2.7 and then I try to > load it in python3, I got this: > Python 3.1.2 (r312:79147, Jul  7 2010, 10:55:24) > [GCC 4.2.1 (Apple Inc. build 5659)] on darwin > Type "help", "copyright", "credits" or "license" for more information. >>>> from acpype import * # this acpype package contains needed class ACTopol >>>> import pickle >>>> o = pickle.load(open('AAA.acpype/AAA.pkl','rb')) > Traceback (most recent call last): >   File "<stdin>", line 1, in <module> >   File "/sw/lib/python3.1/pickle.py", line 1365, in load >    encoding=encoding, errors=errors).load() > TypeError: ('__init__() takes at least 2 positional arguments (1 given)', > <class 'acpype.ACTopol'>, ()) >>>> > Now trying the contrary, pickle file is created with python3, I got this > when trying to load it with python 2.7: > Python 2.7 (r27:82500, Jul  7 2010, 10:48:15) > [GCC 4.2.1 (Apple Inc. build 5659)] on darwin > Type "help", "copyright", "credits" or "license" for more information. >>>> from acpype import * >>>> import pickle >>>> o = pickle.load(open('AAA.pkl','rb')) > Traceback (most recent call last): >   File "<stdin>", line 1, in <module> >   File "/sw/lib/python2.7/pickle.py", line 1378, in load >    return Unpickler(file).load() >   File "/sw/lib/python2.7/pickle.py", line 858, in load >    dispatch[key](self) >   File "/sw/lib/python2.7/pickle.py", line 1083, in load_newobj >    obj = cls.__new__(cls, *args) > AttributeError: class ACTopol has no attribute '__new__' > Apart that, everything else seems to work. Just one note more: when loading > the pickle file 2.7 in python 2.7, type(o) is <type 'instance'>, while > pickle 3 in python 3, type(o) is <class 'acpype.ACTopol'> Your AbstractTopol class needs to inherit from class `object` so that it (and its descendant ACTopol) become new-style classes. Old-style classes don't exist in Python 3, and I think this is at least partly the cause of your problem. Cheers, Chris -- http://blog.rebertia.com |