Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 Date: Wed, 9 Mar 2016 14:28:46 -0700 Lines: 28 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 4/WWTD9tBwCnUqtLB7rzVA9k+C+VF9uFvP8hx0tja6rQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'received:209.85.223': 0.03; 'correct.': 0.07; 'cc:addr:python-list': 0.09; 'dict': 0.09; 'suggest': 0.15; 'wed,': 0.15; '2016': 0.16; 'cc:name:python': 0.16; "he'll": 0.16; 'mismatching': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'similarly,': 0.16; 'tcp': 0.16; 'wrote:': 0.16; 'later': 0.16; "wouldn't": 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'am,': 0.23; 'needed.': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'message- id:@mail.gmail.com': 0.27; 'said,': 0.27; 'pickle': 0.29; 'subject:/': 0.30; 'connection': 0.30; "can't": 0.32; 'right?': 0.33; 'similar': 0.33; 'add': 0.34; 'server': 0.34; 'info': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'returning': 0.35; 'whilst': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'client': 0.37; 'wanted': 0.37; 'doing': 0.38; 'received:209': 0.38; 'easily': 0.39; 'where': 0.40; 'called': 0.40; 'care': 0.60; 'mar': 0.65; 'state,': 0.66; 'therefore': 0.67; 'manner': 0.69; 'restore': 0.70; 'address,': 0.77; 'to:none': 0.91; 'do:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=vCKFLX2dDtnw8yvgSm9Z4MWy5pssWuiFspJJaKpzX64=; b=HB+LjUNG86Gr//YcdfF5L17UUi3klMYuc7THgE6CBKLcFczGL5/4F+RU2JHjtQcSkG Zz7MyE8202iQetMknjXUSoRTBaI2UWfLkG76D4Uy60YkFlY+Uc11krQOpTKcRKfp0l31 Pps3KppMmEPOzIi5zbMtmvUQTrBflD31twyTULt5Yy0Kz1oIRmN2dL16DwOhUT204GpU VIp++Maw2okE4+duyGv2rjVT+/Uizym5acC9Hmlmeds42lQ/KrTc3QuLTyxhLlzgcfso YLd5lW3MWdzw7b2NKfbIOFyBeCXYACLsFjqoyShy21D0BHqfcqeLBo6jtR5qQOJw+pan 4lxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=vCKFLX2dDtnw8yvgSm9Z4MWy5pssWuiFspJJaKpzX64=; b=XpsuEBqKhJSv8JOygV8K72Y+i/7wtlP0fc3k5eqnUWJjhqD1UX2u+us6+QVTTF+OPg UA5FsI3viRy9nN2BZRCj9f+gmeRqaOIalsPtiKLbjr5LEv2BoGzMbLqzT4832Lu4JJt5 e9VbLv68QJgf9+F3ZUnTm8fD0NbFnA31R9AMPgDAWdkencwIFHAs4BJ7yY3TxePEUSlH 4N+qFwe1SC1zw+BFz/NxorpzhBJScx50q/68Bkoxv0TcRwQqk/mE+RIYruIbDtu6QQ5Z OTTPn4geb421JxW/Ro38ju4OHF3USWMziYLO6mOUkHv8d0MZDjZBYW1h+oXndCt2qFyV N6pQ== X-Gm-Message-State: AD7BkJLRhjJ+GT44bMr0r0swDotJ7q4xNZOSmD+CQa89lNtAhPT8+TMuSKvaFwFU9MwpO75FnJB4en8/PE5HyQ== X-Received: by 10.107.19.140 with SMTP id 12mr1576282iot.11.1457558965790; Wed, 09 Mar 2016 13:29:25 -0800 (PST) In-Reply-To: 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:104454 On Wed, Mar 9, 2016 at 2:14 AM, Veek. M wrote: > 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? pickle doesn't care what the return value is, other than it needs to be something picklable. I suggest returning a dict containing the IP address, to make it easier to add additional state later if needed. That said, I wouldn't pickle a TCP client in the first place. You may be able to restore the client state, but you can't easily restore any server state, which could easily lead to mismatching network states. Also, what do you think should happen if the connection fails while unpickling? > Similarly, whilst unpickling, __setstate__ will be called in a manner > similar to this: > x.__setstate__(self, unpickledIP) > > __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? Correct.