Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21271
| Path | csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'attributes': 0.05; 'attribute': 0.07; 'dynamically': 0.09; 'method:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'class,': 0.15; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:which': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'instance': 0.18; 'later': 0.19; '(most': 0.21; 'pointed': 0.21; "doesn't": 0.22; 'breaks': 0.23; 'from:addr:web.de': 0.23; 'traceback': 0.24; 'fix': 0.25; 'import': 0.27; 'script': 0.28; 'fine.': 0.29; 'class': 0.29; 'problem': 0.29; 'skip:b 20': 0.29; 'skip:p 30': 0.29; 'keeps': 0.30; 'but...': 0.30; 'invoke': 0.30; 'tue,': 0.32; 'implement': 0.32; "isn't": 0.33; 'header:User-Agent:1': 0.33; 'it?': 0.33; 'object': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'steven': 0.34; 'rather': 0.34; 'subject:/': 0.34; 'last):': 0.34; 'to:addr:python-list': 0.35; '...': 0.35; 'something': 0.35; 'post': 0.36; 'received:org': 0.36; 'happens': 0.38; 'skip:_ 10': 0.38; 'some': 0.38; 'third': 0.38; 'unless': 0.39; 'point': 0.40; 'change': 0.40; 'to:addr:python.org': 0.40; 'directly.': 0.68; 'restore': 0.68; '-0500,': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: pickle/unpickle class which has changed |
| Date | Tue, 06 Mar 2012 15:28:16 +0100 |
| Organization | (None) |
| References | <mailman.422.1331037292.3037.python-list@python.org> <4f561740$0$29989$c3e8da3$5496439d@news.astraweb.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p50849614.dip.t-dialin.net |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.426.1331044111.3037.python-list@python.org> (permalink) |
| Lines | 88 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1331044111 news.xs4all.nl 6872 [2001:888:2000:d::a6]:57377 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:21271 |
Show key headers only | View raw
Steven D'Aprano wrote: > On Tue, 06 Mar 2012 07:34:34 -0500, Neal Becker wrote: > >> What happens if I pickle a class, and later unpickle it where the class >> now has added some new attributes? > > Why don't you try it? > > py> import pickle > py> class C: > ... a = 23 > ... > py> c = C() > py> pickled = pickle.dumps(c) > py> C.b = 42 # add a new class attribute > py> d = pickle.loads(pickled) > py> d.a > 23 > py> d.b > 42 > > > Unless you mean something different from this, adding attributes to the > class is perfectly fine. > > But... why are you dynamically adding attributes to the class? Isn't that > rather unusual? The way I understand the problem is that an apparently backwards-compatible change like adding a third dimension to a point with an obvious default breaks when you restore an "old" instance in a script with the "new" implementation: >>> import pickle >>> class P(object): ... def __init__(self, x, y): ... self.x = x ... self.y = y ... def r2(self): ... return self.x*self.x + self.y*self.y ... >>> p = P(2, 3) >>> p.r2() 13 >>> s = pickle.dumps(p) >>> class P(object): ... def __init__(self, x, y, z=0): ... self.x = x ... self.y = y ... self.z = z ... def r2(self): ... return self.x*self.x + self.y*self.y + self.z*self.z ... >>> p = P(2, 3) >>> p.r2() 13 >>> pickle.loads(s).r2() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 7, in r2 AttributeError: 'P' object has no attribute 'z' By default pickle doesn't invoke __init__() and updates __dict__ directly. As pointed out in my previous post one way to fix the problem is to implement a __setstate__() method: >>> class P(object): ... def __init__(self, x, y, z=0): ... self.x = x ... self.y = y ... self.z = z ... def r2(self): ... return self.x*self.x + self.y*self.y + self.z*self.z ... def __setstate__(self, state): ... self.__dict__["z"] = 42 # stupid default ... self.__dict__.update(state) ... >>> pickle.loads(s).r2() 1777 This keeps working with pickles of the new implementation of P: >>> q = P(3, 4, 5) >>> pickle.loads(pickle.dumps(q)).r2() 50
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
pickle/unpickle class which has changed Neal Becker <ndbecker2@gmail.com> - 2012-03-06 07:34 -0500
Re: pickle/unpickle class which has changed Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-06 13:55 +0000
Re: pickle/unpickle class which has changed Peter Otten <__peter__@web.de> - 2012-03-06 15:28 +0100
Re: pickle/unpickle class which has changed Neal Becker <ndbecker2@gmail.com> - 2012-03-06 11:29 -0500
Re: pickle/unpickle class which has changed Peter Otten <__peter__@web.de> - 2012-03-06 18:10 +0100
csiph-web