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: 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> 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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.