Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: problem using pickle Date: Sun, 13 Mar 2016 14:24:05 +0100 Organization: None Lines: 57 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.uni-berlin.de rmqncESr6lHWI/35cnzFogoIRpSLMIyqLopqkSqOWQHw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'filename': 0.07; 'attempted': 0.09; 'here?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:using': 0.09; 'python': 0.10; 'def': 0.13; '........': 0.16; 'built-in;': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'stuff.': 0.16; 'world;': 0.16; 'wrote:': 0.16; 'team,': 0.18; 'subject:problem': 0.22; 'suppose': 0.22; 'code.': 0.23; 'defined': 0.23; 'import': 0.24; 'script': 0.25; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:_ 20': 0.26; 'error': 0.27; 'actual': 0.28; 'pickle': 0.29; 'post': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'instances': 0.33; "skip:' 20": 0.34; 'file': 0.34; 'instance': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'created': 0.36; 'data.': 0.36; 'smaller': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'mac': 0.37; 'wrong': 0.38; 'several': 0.38; 'skip:p 20': 0.38; 'thank': 0.38; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'avoid': 0.61; 'provide': 0.61; 'you.': 0.64; 'outline': 0.66; 'cut': 0.67; 'dear': 0.67; 'skip:\xe2 10': 0.70; '.....': 0.76; "'object'": 0.84; 'endeavour': 0.84; 'object:': 0.84; 'redefining': 0.91; 'world:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8f80.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104766 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.