Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: dieter Newsgroups: comp.lang.python Subject: Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 Date: Thu, 10 Mar 2016 09:06:06 +0100 Lines: 45 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: news.uni-berlin.de hKve8iY5kcFLasUCYxxSNACbcCb21zIF1eOpb/uDdiNA== Cancel-Lock: sha1:BWKRlY8U1/ssJODMYHVVKLhA3O8= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'builtin': 0.07; 'explicitely': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; '(either': 0.16; "he'll": 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; 'similarly,': 0.16; 'object.': 0.22; 'this:': 0.23; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; '(e.g.': 0.27; 'compare': 0.27; 'pickle': 0.29; 'allows': 0.30; 'subject:/': 0.30; 'right?': 0.33; 'similar': 0.33; 'info': 0.34; 'could': 0.35; 'fail': 0.35; 'whilst': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'client': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'wanted': 0.37; 'things': 0.38; 'doing': 0.38; 'skip:v 20': 0.38; 'skip:p 20': 0.38; 'whatever': 0.39; 'does': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'called': 0.40; 'results.': 0.67; 'therefore': 0.67; 'manner': 0.69; 'restore': 0.70; 'yourself': 0.73; 'expect.': 0.84; 'returns.': 0.84; 'do:': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57b38185.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) 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:104483 "Veek. M" writes: > ... > what i wanted to know was, x = Client('192.168.0.1') will create an > object 'x' with the IP inside it. When I do: > pickle.dump(x) > pickle doesn't know where in the object the IP is, so he'll call > __getstate__ and expect the return value to be the IP, right? It does not expect anything. It pickles, whatever "__getstate__" returns. The unpickling will fail (either explicitely or implicitely), when "__setstate__" does not "fit" with "__getstate__" or does not restore the state in the way you expect. > Similarly, whilst unpickling, __setstate__ will be called in a manner > similar to this: > x.__setstate__(self, unpickledIP) Yes. > __setstate__ can then populate 'x' by doing > self.x = str(unpickledIP) > > the type info is not lost during pickling is it, therefore 'str' is not > required is it? Yes. Note however, that "__setstate__" is called automatically during "unpickling". No need that you do this yourself. You can also easily try out things yourself (e.g. in an interactive Python session). The builtin "vars" allows you to look into an object. As an example, you could do: client = Client(...) vars(client) pickle.dump(client, open(fn, "wb")) .... recreated_client = pickle.load(open(fn, "rb")) vars(recreated_client) and then compare the two "vars" results.