There are three different namespaces in Python: a local namespace, a module namespace and a global namespace. The latter contains all built-in functions. The module namespace contains all the function definitions and variables of a module. It can be accessed using the . (dot) operator. A local environment is created at function calls. It includes all the parameters and local variables of the function. Function definitions can be nested, and nested functions have their own local namespace.
Example 4.7. Function execution namespaces
>>> enz = []
>>> def add_enz(*new):
... def verif():
... print "enz: ", enz
... print "new: ", new
... verif()
... enz.extend(list(new))
>>> add_enz('EcoRI')
enz: []
new: ('EcoRI',)
>>> enz
[ 'EcoRI' ]
![]() | Caution |
|---|---|
This behaviour only exists in Python version 2.2. Previous versions have only one function execution namespace. In this case, the new variable in Example 4.7 is not accessible within the verif function. | |
Variable names are resolved by searching the namespaces in the following order: local namespaces (function execution namespaces potentially nested), current module namespace and global namespace containing built-in definitions.
When object methods or attributes are addressed using the . (dot) operator, namespaces searching is different. Each object has its own local namespace implemented as a dictionary named __dict__. This dictionary is searched for the name following the . (dot) operator. If it is not found, the local namespace of its class, accessible via the __class__ attribute, is searched for. If it is not found there, a lookup on the parent classes is performed. Since modules are objects, accessing the namespace of a module use the same mechanism.
>>> enz = ['EcoRI']
>>> enz.__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'list' object has no attribute '__dict__'
>>> enz.__class__.__dict__
<dict-proxy object at 0x815776c>
>>> print enz.__class__.__dict__
{'sort': <method 'sort' of 'list' objects>,
'__ne__': <slot wrapper '__ne__' of 'list' objects>,
'reverse': <method 'reverse' of 'list' objects>,
'__getslice__': <slot wrapper '__getslice__' of 'list' objects>,
'insert': <method 'insert' of 'list' objects>,
'__len__': <slot wrapper '__len__' of 'list' objects>,
'__getattribute__': <slot wrapper
'__getattribute__' of 'list' objects>,
'remove': <method 'remove' of 'list' objects>,
'append': <method 'append' of 'list' objects>,
'__setitem__': <slot wrapper '__setitem__' of 'list' objects>,
'pop': <method 'pop' of 'list' objects>,
'__add__': <slot wrapper '__add__' of 'list' objects>,
'__gt__': <slot wrapper '__gt__' of 'list' objects>,
'__rmul__': <slot wrapper '__rmul__' of 'list' objects>,
'__lt__': <slot wrapper '__lt__' of 'list' objects>,
'__eq__': <slot wrapper '__eq__' of 'list' objects>,
'__init__': <slot wrapper '__init__' of 'list' objects>,
'__imul__': <slot wrapper '__imul__' of 'list' objects>,
'extend': <method 'extend' of 'list' objects>,
'__delitem__': <slot wrapper '__delitem__' of 'list' objects>,
'__delslice__': <slot wrapper '__delslice__' of 'list' objects>,
'__getitem__': <slot wrapper '__getitem__' of 'list' objects>,
'__contains__': <slot wrapper '__contains__' of 'list' objects>,
'index': <method 'index' of 'list' objects>,
'__setslice__': <slot wrapper '__setslice__' of 'list' objects>,
'count': <method 'count' of 'list' objects>,
'__iadd__': <slot wrapper '__iadd__' of 'list' objects>,
'__le__': <slot wrapper '__le__' of 'list' objects>,
'__repr__': <slot wrapper '__repr__' of 'list' objects>,
'__hash__': <slot wrapper '__hash__' of 'list' objects>,
'__new__': <built-in method __new__ of type object at 0x80f1aa0>,
'__doc__': "list() -> new list\nlist(sequence) -> new list
initialized from sequence's items",
'__ge__': <slot wrapper '__ge__' of 'list' objects>,
'__mul__': <slot wrapper '__mul__' of 'list' objects>}
>>> dir (enz)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__',
'__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__',
'__imul__', '__init__', '__le__', '__len__', '__lt__', '__mul__',
'__ne__', '__new__', '__reduce__', '__repr__', '__rmul__', '__setattr__',
'__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend',
'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
![]() | Go back |
|---|---|
|
Return to Section 2.7. | |