problem using pickle

Peter Otten __peter__ at web.de
Sun Mar 13 09:24:05 EDT 2016


Nicky Mac wrote:

> Dear Python team,
> I have studied the excellent documentation, 

That is always a laudable endeavour ;)

> and attempted to make use of
> pickle thus:
> 
> filename = 'my_saved_adventure'
> import  pickle
> class object:

'object' already is the name for a Python built-in; you should avoid 
redefining it.

>     def __init__(self,i,.........t) :
>         self.id       = i
>          .....
> 
> class world:
>     def __init__(self):
>         self.object
> 
> class object:

As you already defined 'object' above you now have a name clash with your 
own stuff. I suppose that is because what you post is not your actual code.

>     def __init__(self,i,
> 
> .....then   Object instances of object are created ........
> 
>         myworld = world;
>         myworld.Object = Object
>         fileobj = open(filename,'wb')
>         pickle.dump(myworld,fileobj); fileobj.close()
>         result = "saved your game to " + filename
> 
>         fileobj = open(filename,'rb')
>         myworld = pickle.load(fileobj); fileobj.close()
>         Object = myworld.Object
>         result = "restored your game from " + filename
> 
> 
> The proecedures execute without error
> but a file of only 21b is created containing " €c__main__world q .
> altho there are several k of object instance data.

Unfortunately I cannot tell what might have gone wrong from the outline you 
provide above. Can you make a smaller script that shows the same problem, 
and that you can post in its entirety here? Use cut and paste, do not 
retype! Thank you.






More information about the Python-list mailing list