M
Michele Simionato
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.
$ 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.