Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #18294
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: pickling instances of metaclass generated classes |
| Date | 2012-01-01 13:51 +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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.4291.1325422280.27778.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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