Namespaces in Python
What's a namespace?
In simple words, A namespace
in python is a collection of names and the details of the objects
represented by the names. We can consider a namespace as a python wordbook
which maps object names to objects. The keys of the wordbook correspond to the
names and the values correspond to the objects in python.
In python, there are four kinds of namespaces, namely built-in
namespaces, global namespaces, original namespaces and enclosing namespaces.
We'll study about each of them in the coming portions.
Global Namespace
The global namespace contains any names defined at the position of the
main program. Python creates the global namespace when the main program body
starts, and it remains in actuality until the practitioner terminates.
Local — Hunt for the name in the local namespace of the function
Enclosing — If it’s not present in the local namespace, hunt for it in
the scope of the enclosing function
Main — If it’s not present in global, explore the built-ins and, main,
namespaces
Scope
A Scope is a textual area of a Python program where a Namespace is
directly accessible. Any direct source to a name with in a scope is straightway
looked up in the namespace the scope has access to.
Python Built-in Scope
The built-in scope contains all of the Python functions that are built-in
to vanilla Python. These include familiar functions similar as print and dir. A
list of all the Python built-in functions can be establish then whenever one of
the built-in functions is prescribed, Python will search until it finds the
name of the function in the built-in scope’s namespace. The built-in compass is
the widest scope Python uses, which means that if any name within the built-in
scope is defined again within a lower scope, also the name within the smaller
scope will be used. This can be adverse if we use a built-in name as a new
variable.
Also learn about Tuples in
Python in Blog.
Comments
Post a Comment