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


Groups > comp.lang.python > #18294

Re: pickling instances of metaclass generated classes

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'mentioned,': 0.04; 'sys': 0.05; 'assert': 0.09; 'instance.': 0.09; 'module?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; '(),': 0.16; 'cache,': 0.16; 'fix,': 0.16; 'gmane': 0.16; 'home:': 0.16; 'name):': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:classes': 0.16; 'usenet': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'seems': 0.20; 'cheers,': 0.20; 'posting': 0.20; 'assigning': 0.23; 'from:addr:web.de': 0.23; 'module,': 0.23; 'module': 0.26; 'import': 0.27; 'work.': 0.28; 'van': 0.28; 'pass': 0.29; 'sorry,': 0.29; 'problem': 0.29; 'print': 0.29; 'error': 0.29; 'class': 0.29; "i've": 0.31; "didn't": 0.31; 'does': 0.32; 'ps:': 0.32; 'yet': 0.32; 'objects': 0.32; 'this.': 0.33; 'header:X-Complaints-To:1': 0.33; 'to:addr :python-list': 0.34; 'probably': 0.34; 'follow-up': 0.34; 'post': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'skip:_ 10': 0.37; 'received:org': 0.38; 'put': 0.38; 'manually': 0.39; 'should': 0.39; 'to:addr:python.org': 0.40; 'happen': 0.61; 'further': 0.64; '"spam"': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: pickling instances of metaclass generated classes
Date Sun, 01 Jan 2012 13:51:05 +0100
Organization None
References <2d221d0f-458e-4821-8786-f064b44b3125@p16g2000yqd.googlegroups.com> <mailman.4232.1325188538.27778.python-list@python.org> <c3502a73-d295-4f71-b945-c4decd079151@r5g2000yqc.googlegroups.com> <jdk805$263$1@dough.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084953a.dip.t-dialin.net
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.4291.1325422280.27778.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1325422280 news.xs4all.nl 6987 [2001:888:2000:d::a6]:36060
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18294

Show key headers only | View raw


lars van gemerden wrote:

>> import pickle
>> import sys
>>
>> class MetaClass(type):
>>     pass
>>
>> class M(object):
>>     def __init__(self, module):
>>         self.__module = module
>>     def __getattr__(self, name):
>>             print "creating class", name
>>             class_ = MetaClass(name, (), {"__module__": self.__module})
>>             setattr(self, name, class_)
>>             return class_
>>
>> sys.modules["m"] = M("m")
>> import m
>> c = m.x
>> s = pickle.dumps(c)
>> print repr(s)
>> d = pickle.loads(s)
>>
>> assert c is d
>>
>> sys.modules["m"] = M("m")
>> e = pickle.loads(s)
>>
>> assert c is not e
>>
>> The official way is probably what Robert mentioned, via the copy_reg
>> module, but I didn't get it to work.
> 
> I will look further into this. does "sys.modules["m"] = M("m")" create
> a new module?

Assigning to sys.modules[modulename] can put arbitrary objects into the 
module cache, in this case an M instance. To drive the point home:

>>> import sys
>>> sys.modules["x"] = 42
>>> import x
>>> x
42
>>> sys.modules["x"] = "spam"
>>> import x
>>> x
'spam'

> Cheers, Lars
> 
> PS: I get an error when posting this to the usenet group

Sorry, that seems to happen when I post via gmane and don't manually clear 
the follow-up that my newsreader helpfully (knode) inserts. I've not yet 
found a permanent fix, but if that was the problem you should be able to 
answer this post.

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


Thread

pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-29 01:55 -0800
  Re: pickling instances of metaclass generated classes Robert Kern <robert.kern@gmail.com> - 2011-12-29 11:08 +0000
  Re: pickling instances of metaclass generated classes Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-29 12:55 -0700
    Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 01:50 -0800
      Re: pickling instances of metaclass generated classes Peter Otten <__peter__@web.de> - 2011-12-30 12:41 +0100
      Re: pickling instances of metaclass generated classes Peter Otten <__peter__@web.de> - 2012-01-01 13:51 +0100
    Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 03:16 -0800
      Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 07:56 -0800
        Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2011-12-30 08:51 -0800
          Re: pickling instances of metaclass generated classes Ian Kelly <ian.g.kelly@gmail.com> - 2011-12-30 10:09 -0700
  Re: pickling instances of metaclass generated classes lars van gemerden <lars@rational-it.com> - 2012-01-03 03:43 -0800

csiph-web