Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #104483

Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172

From dieter <dieter@handshake.de>
Newsgroups comp.lang.python
Subject Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172
Date 2016-03-10 09:06 +0100
Message-ID <mailman.112.1457597177.15725.python-list@python.org> (permalink)
References <nbjt3b$69k$1@dont-email.me> <mailman.71.1457511067.15725.python-list@python.org> <nbopc0$3eh$1@dont-email.me>

Show all headers | View raw


"Veek. M" <vek.m1234@gmail.com> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 "Veek. M" <vek.m1234@gmail.com> - 2016-03-07 18:17 +0530
  Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 dieter <dieter@handshake.de> - 2016-03-09 09:10 +0100
    Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 "Veek. M" <vek.m1234@gmail.com> - 2016-03-09 14:44 +0530
      Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-09 14:28 -0700
        Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 "Veek. M" <vek.m1234@gmail.com> - 2016-03-10 06:31 +0530
      Re: Pickle __getstate__ __setstate__ and restoring n/w - beazley pg 172 dieter <dieter@handshake.de> - 2016-03-10 09:06 +0100

csiph-web