Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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; 'subsequent': 0.04; 'cache': 0.05; 'newbie': 0.05; '__name__': 0.07; 'defines': 0.07; 'key.': 0.07; 'python': 0.09; 'func': 0.09; 'imports': 0.09; 'library?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'library': 0.15; 'dec': 0.15; "'__main__':": 0.16; '2.7.3': 0.16; 'a()': 0.16; 'dictionary,': 0.16; 'dummy': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'registry': 0.16; 'subject:dictionaries': 0.16; 'surprising': 0.16; 'sys.modules': 0.16; 'trap': 0.16; 'ubuntu.': 0.16; 'wrote:': 0.17; 'implementing': 0.17; 'instance': 0.17; 'module,': 0.17; "shouldn't": 0.17; 'module': 0.19; 'import': 0.21; 'script': 0.24; 'pass': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'header:X-Complaints-To:1': 0.28; 'assert': 0.29; 'cat': 0.29; 'dictionary': 0.29; 'falls': 0.29; 'class': 0.29; 'expect': 0.31; 'file': 0.32; 'print': 0.32; 'traceback': 0.33; 'to:addr:python- list': 0.33; 'another': 0.33; 'received:org': 0.36; 'skip:{ 10': 0.36; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'real': 0.61; 'different': 0.63; 'effects,': 0.84; 'seriously,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: surprising behaviour of global dictionaries Date: Tue, 09 Oct 2012 17:24:18 +0200 Organization: None References: <40ac555b-71c2-445e-814b-6bd1d037dbee@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a243.dip.t-dialin.net User-Agent: KNode/4.7.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: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349796256 news.xs4all.nl 6846 [2001:888:2000:d::a6]:59518 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31038 Michele Simionato wrote: > I have the following module implementing a registry of functions with a > decorator: > > $ cat x.py > registry = {} # global dictionary > > def dec(func): > registry[func.__name__] = func > print registry, id(registry) > return func > > if __name__ == '__main__': > import xlib > print registry, id(registry) > > The library xlib just defines two dummy functions: > > $ cat xlib.py > from x import dec > > @dec > def f1(): > pass > > @dec > def f2(): > pass > > Then I get the following output: > > $ python x.py > {'f1': } 27920352 > {'f1': , 'f2': {0x7f7bce0cd6e0>} 27920352 } 27395472 > > This is surprising since I would expect to have a single global > dictionary, not two: how comes the registry inside the ``if __name__ == > '__main__'`` block is different from the one seen in the library? > > This is python 2.7.3 on Ubuntu. Welcome to python -- this is a trap every newbie falls into ;) Seriously, you shouldn't use the main script as a library; it is put into the sys.modules cache under the "__main__" key. Subsequent imports under its real name will not find that name in the cache and import another instance of the module, with puzzling effects, like $ cat x.py import x class A: pass a = A() assert isinstance(a, x.A) $ python x.py Traceback (most recent call last): File "x.py", line 4, in assert isinstance(a, x.A) AssertionError $