tricky cPickling

Gerrit Holl gerrit.holl at pobox.com
Thu Feb 17 14:58:11 EST 2000


Etienne Antoniutti Di Muro wrote on 950810383:
> This is my first message posted, so hi everybody !! :-)
> 
> 
> Recently I have experienced some problems using cPickle.load function.
> Here it comes:
> code is pretty simple:
> 
> class MyStuff:
>         def save(filename):

In classes, the first argument of a method must be 'self'. This is
needed to access instance-depentent attributes.

>                 obj = (GlobalVar1, GlobalVar2, GlobalVar3) #GlobalVar
> are instances of classes
>                 f = open(filename, "w")
>                 cPickle.dump(f, obj)
>                 f.close()
> 
>    def load(filename):

And the indentation is not consistent.

>            f = open(filename, "r")
>           (globalVar1, globalVar2, globalVar3) = cPickle.load(f)
>           f.close()
>           return obj
> 
> I experiece errors in unpickling only (MyStuff.load("MyFileName")), they
> are listed as follows:
> 
> 1_ NameErrors of some objects from modules used by the "GlobalVar"
> cPickled. (but please note that those modules are properly imported
> before cPickling);

You should type 'self.GlobalVar1' and create a first argument 'self'
to the methods

> 3_ Different behaviours (raised exceptions) of the script which uses
> MyStuff.load("MyFileName") method, according how i call it. Is it due to
> different exception managment of MacPython IDE instead of MacPython
> AppletBuilder ??

class Foo:
    def print_something(self, message):
        print "instance %s is printing: %s" % (self, message)

> 4_ Cause of those errors I can't embed MyClass in a C function, that is
> where i need it to work.
> 
> Any Suggestion ??

Yes :)

regards,
Gerrit.

-- 
cat: /home/gerrit/.signature: No such quote or joke




More information about the Python-list mailing list