Path: csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'attribute': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; "'c'": 0.16; '*instance*': 0.16; 'attribute"': 0.16; 'attribute.': 0.16; 'attributes.': 0.16; 'b):': 0.16; 'b=2)': 0.16; 'c):': 0.16; 'mutable': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:object': 0.16; 'ignore': 0.16; 'fix': 0.17; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'load': 0.23; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'header:X-Complaints-To:1': 0.27; 'appear': 0.29; '"",': 0.31; 'object.': 0.31; 'pickle': 0.31; 'file': 0.32; 'class': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'problem': 0.35; 'classes': 0.35; 'problem.': 0.35; 'definition': 0.35; 'test': 0.35; 'but': 0.35; 'instances': 0.36; 'list': 0.37; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'read': 0.60; 'easy': 0.60; 'free': 0.61; 'affect': 0.61; 'new': 0.61; '20,': 0.68; 'smith': 0.68; 'introduce': 0.78; 'pickled': 0.84; 'want:': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: pickle and then object changes Date: Tue, 20 Jan 2015 19:41:25 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd88a7.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 87 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421779297 news.xs4all.nl 2939 [2001:888:2000:d::a6]:43038 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84084 James Smith wrote: > Say an object like this exists: > class test: > a = "" > b = "" > > You pickle it. > > You change the object definition to have a new field: > class test > a = "" > b = "" > c = "" > > You read the pickled object. > Will it load but ignore the new field? > That is what I want. Classes are pickled by name. You are free to replace them with whatever you want: >>> class Test: ... a = 1 ... b = 2 ... >>> import pickle >>> p = pickle.dumps(Test) >>> class Test: ... a = 10 ... b = 20 ... c = 30 ... >>> P = pickle.loads(p) >>> P.c 30 >>> P.a 10 >>> P is Test True If you introduce a new *instance* attribute that will not magically appear on unpickling: >>> class T: ... def __init__(self, a, b): ... self.a = a ... self.b = b ... def __str__(self): return "T(a={0.a}, b={0.b})".format(self) ... >>> t = T(1, 2) >>> print(t) T(a=1, b=2) >>> p = pickle.dumps(t) >>> class T: ... def __init__(self, a, b, c): ... self.a = a ... self.b = b ... self.c = c ... def __str__(self): return "T(a={0.a}, b={0.b}, c={0.c})".format(self) ... >>> print(T(10, 20, 30)) T(a=10, b=20, c=30) >>> v = pickle.loads(p) No problem so far as the initializer is not called so that the missing c is not a problem. But then: >>> print(v) Traceback (most recent call last): File "", line 1, in File "", line 6, in __str__ AttributeError: 'T' object has no attribute 'c' One easy fix is to use a class attribute as the fallback: >>> T.c = "class attribute" >>> print(v) T(a=1, b=2, c=class attribute) But remember not to try this with mutable attributes. When `c` is a list v.c.append(42) will affect all instances with a missing `c` instance attribute.