Wednesday, 11 July 2018

Python Namespace

Cited from the book "Python How to Program"

Namespaces store information about an identifier and the value to which it is bound. Python defines three namespaces: local, global and built-in. When a program attempts to access an identifier’s value, Python searches the namespaces in a certain order—local, global and built-in namespaces—to see whether and where the identifier exists.

If the function’s local namespace does not contain an identifier named x (e.g., the function does not define any parameters or create any identifiers named x), Python searches the next outer namespace: the global namespace (sometimes called the module namespace).

The global namespace contains the bindings for all identifiers, function names and class names defined within a module or file. Each module or file’s global namespace contains an identifier called __name__ that states the module’s name (e.g., "math" or "random"). When a Python interpreter session starts or when the Python interpreter begins executing a program stored in a file, the value of __name__ is "__main__".

The built-in namespace contains identifiers that correspond to many Python functions and error messages. For example, functions raw_input, int and range belong to the built-in namespace. Python creates the built-in namespace when the interpreter starts, and programs normally do not modify the namespace (e.g., by adding an identifier to the namespace). Identifiers contained in built-in namespaces may be accessed by code in programs, modules or functions.

Python provides a way for programmers to determine what identifiers are available from the current namespace. Built-in function dir returns a list of these identifiers.

Python provides many other introspective capabilities, including functions globals and locals that return additional information about the global and local namespaces, respectively.

>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'math']
>>> print math
<module 'math' (built-in)>
>>> dir( math )
['__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil',
'cos', 'cosh','e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hy-
pot', 'ldexp', 'log', 'log10','modf', 'pi', 'pow', 'sin', 'sinh',
'sqrt', 'tan', 'tanh']

Python provides the from/import statement to import one or more identifiers from a module directly into the program’s namespace.

When the interpreter executes the line

from math import sqrt

the interpreter creates a reference to function math.sqrt and places the reference directly into the session’s namespace. Now, we can call the function directly without using the dot operator.

>>> from math import sqrt
>>> dir()
['__builtins__', '__doc__', '__name__', 'sqrt']
>>> sqrt( 9.0 )
3.0
>>> from math import sin, cos, tan
>>> dir()
['__builtins__', '__doc__', '__name__', 'cos', 'sin', 'sqrt',
'tan']

The statement

from math import *

imports all identifiers that do not start with an underscore from the math module into the interactive session’s namespace.

>>> import random as randomModule
>>> dir()
['__builtins__', '__doc__', '__name__', 'randomModule']
>>> randomModule.randrange( 1, 7 )
1
>>> from math import sqrt as squareRoot
>>> dir()
['__builtins__', '__doc__', '__name__', 'randomModule', 'square-
Root']
>>> squareRoot( 9.0 )
3.0






No comments:

Post a Comment