Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31038
| 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 | <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; '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 <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.2006.1349796256.27098.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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': <function f1 at 0x7f7bce0cd668>} 27920352
> {'f1': <function f1 at 0x7f7bce0cd668>, 'f2': <function f2 at
> {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 <module>
assert isinstance(a, x.A)
AssertionError
$
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
surprising behaviour of global dictionaries Michele Simionato <michele.simionato@gmail.com> - 2012-10-09 08:08 -0700
Re: surprising behaviour of global dictionaries Peter Otten <__peter__@web.de> - 2012-10-09 17:24 +0200
Re: surprising behaviour of global dictionaries Michele Simionato <michele.simionato@gmail.com> - 2012-10-09 08:36 -0700
Re: surprising behaviour of global dictionaries Dave Angel <d@davea.name> - 2012-10-09 12:10 -0400
Re: surprising behaviour of global dictionaries Michele Simionato <michele.simionato@gmail.com> - 2012-10-09 08:36 -0700
Re: surprising behaviour of global dictionaries Grant Edwards <invalid@invalid.invalid> - 2012-10-09 15:53 +0000
Re: surprising behaviour of global dictionaries Peter Otten <__peter__@web.de> - 2012-10-09 19:45 +0200
csiph-web