8.3. Functions and namespaces

In the following piece of code, and even if it is confusing at the beginning, the seq variable in the function call is not the same as the seq parameter of the gc function. During the function call the global variable is evaluated and its value 'ATGACT' is assigned to the parameter seq of the gc function.

>>> seq = 'ATGACT'
>>> def gc(seq):
      ....
>>> gc(seq)
	

How does Python distinguish the two variables? It can do so, because they are not in the same namespace. All functions have two associated namespaces, a local and a global namespace. The global namespace is the namespace where the function is defined and the local namespace is a namespace created at the function definition where parameters are defined. The local namespace contains also all local variables. Local variables are variables that are bound to value in the function body. When a function is defined, its source code is analyzed and all used names are sorted in the two namespaces.

If a variable name is searched during a function execution, Python looks in the local namespace for local variables and in the global namespace for global variables. If a global variable is not found there, the builtin namespace is searched. If it is not found there, an error is produced.

builtin namespace

The builtin namespace is a namespace that contains all predefined function of Python.

You can ask Python which names are defined in the global and the local namespace using the functions globals() and locals().

	>>> from string import *
>>> def gc(seq):
...    nbases = count(seq, 'N')
...    gcpercent = float(count(seq, 'G') + count(seq, 'C')) / (len(seq) - nbases)
...    print "local namespace:", locals()
...    print "global namespace:", globals()
...    print "names in the current namespace:", dir()
...    return gcpercent
...
>>> seq = 'ATGACGATAGGAGANNTATAGAN'

>>> gc(seq)
local namespace: {'gcpercent': 0.34999999999999998, 'nbases': 3, 
'seq': 'ATGACGATAGGAGANNTATAGAN'}
global namespace: {'ascii_lowercase': 'abcdefghijklmnopqrstuvwxyz', 
	'upper': <function upper at 0x1623f0>, 'punctuation': '"!#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~', 
'letters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 
'seq': 'ATGACGATAGGAGANNTATAGAN', 'lstrip': <function lstrip at 0x160cf0>, 
'uppercase': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
'ascii_letters': 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 
'replace': <function replace at 0x160b90>, 'capwords': <function capwords at 0x160b10>, 
'index_error': <class exceptions.ValueError at 0x119700>, 
'expandtabs': <function expandtabs at 0x160a50>, 'strip': <function strip at 0x160cb0>, 
'ascii_uppercase': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'find': <function find at 0x160810>, 
'gc': <function gc at 0x160b50>, 'rjust': <function rjust at  0x160990>, 
'ljust': <function ljust at 0x160950>, 'whitespace': '\t\n\x0b\x0c\r ', 
'rindex': <function rindex at 0x1625a0>, 'capitalize': <function capitalize at 0x160ad0>, 
'atol_error': <class exceptions.ValueError at 0x119700>, 'octdigits': '01234567', 
'lower': <function lower at 0x1623b0>, 'splitfields': <function split at 0x160d70>, 
'split': <function split at 0x160d70>, 'rstrip': <function rstrip at 0x160d30>, 
'translate': <function translate at 0x160a90>, 
'__doc__': None, 
'printable': '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c', 
'digits': '0123456789', 'joinfields': <function join at 0x162520>, 
'index': <function index at 0x162560>, 
'__builtins__': <module '__builtin__' (built-in)>, 
'swapcase': <function swapcase at 0x162430>, 
'atof_error': <class exceptions.ValueError at 0x119700>, 
'atoi': <function atoi at 0x1608d0>, 'hexdigits': '0123456789abcdefABCDEF', 
'atol': <function atol at 0x160910>, '__name__': '__main__', 
'atof': <function atof at 0x160890>, 'count': <function count at 0x1625e0>, 
'lowercase': 'abcdefghijklmnopqrstuvwxyz', 'join': <function join at 0x162520>, 
'center': <function center at 0x1609d0>, 'rfind': <function rfind at 0x160850>, 
'atoi_error': <class exceptions.ValueError at 0x119700>, 
'maketrans': <built-in function maketrans>, 'zfill': <function zfill at 0x160a10>}
names in the current namespace: ['gcpercent', 'nbases', 'seq']
0.34999999999999998

      

The dir function shows all the names defined in the current namespace.

>>> from string import *

>>> dir()
['__builtins__', '__doc__', '__name__', 'ascii_letters', 'ascii_lowercase', 
'ascii_uppercase', 'atof', 'atof_error', 'atoi', 'atoi_error', 'atol', 
'atol_error', 'capitalize', 'capwords', 'center', 'count', 'digits', 
'expandtabs', 'find', 'gc', 'hexdigits', 'index', 'index_error', 'join', 
'joinfields', 'letters', 'ljust', 'lower', 'lowercase', 'lstrip', 'maketrans', 
'octdigits', 'printable', 'punctuation', 'replace', 'rfind', 'rindex', 'rjust', 
'rstrip', 'seq', 'split', 'splitfields', 'strip', 'swapcase', 'translate', 
'upper', 'uppercase', 'whitespace', 'zfill']