In this lesson, you will learn about the namespace, the mapping of names to objects, and the scope of a variable, with examples to better understand the topic.
Namespaces are one excellent concept — let’s do more of those; the final line reads in “The Zen of Python” (type import this in the Python interpreter). So what exactly are these enigmatic namespaces? Let’s start by examining what the name is.
Python treats everything as an object. All an object has is its name, often known as an identifier, and the underlying object can be accessed using its name. So, a namespace is a collection of currently defined symbolic names and details about the objects to which each name refers. You can compare a namespace to a dictionary where the keys are the names of the things, and the values are the actual objects.
Through the built-in function id, we may obtain the address (in RAM) of some object (). For instance, when we perform the assignment, mathMarks = 99
, the name we give it to, and 99 is an object stored in memory. Let’s examine its application.
Example
mathMarks = 1 print('id(mathMarks) =', id(mathMarks)) mathMarks = mathMarks+1 print('id(mathMarks) =', id(mathMarks)) print('id(3) =', id(3)) engmarks = 2 print('id(engmarks) =', id(engmarks)) print('id(2) =', id(2))
Output
id(mathMarks) = 9788992 id(mathMarks) = 9789024 id(3) = 9789056 id(engmarks) = 9789024 id(2) = 9789024
As you can see in the above example, you may get different values for the id.
When we do mathMarks = mathMarks + 1
, a new object 3 is formed, and now the name is associated with this object. Initially, object 2 is generated, and the name is associated with it. Note that the values of id(mathMarks)
and id(3)
are identical.
Furthermore, the new name engmarks is connected to the prior object 2 when engmarks = 2
is executed. Because Python doesn’t have to construct a new duplicate object, this is efficient. Python is strong because of the dynamic nature of name binding, which allows names to refer to any object.
i = 500 i = 'Hello World!' i = [100,200,300]
All these are acceptable, and a will, in various contexts, refers to three distinct categories of objects. Since functions are objects, their names are used to identify them.
def mathMarks(): print("100") m = mathMarks m()
Output
100
We can discuss namespaces now that we are familiar with the definition of names.
id()
, print()
, and others from any portion of the program. Every module establishes its global namespace.Although many distinct namespaces have been declared, not all of them may be accessible from every part of the program. The idea of scope is involved, and you can access a namespace straight from a program’s scope without needing a prefix in Python.
In Python, at least three nested scopes are always active.
A name is first searched in the local namespace, then in the global namespace, and finally in the built-in namespace when a reference is made inside a function. A new scope is nested inside the local scope if a function is contained within another.
Example
def outer_function(): hisMarks = 80 def inner_function(): engMarks = 90 mathMarks =100
The variable mathMarks
in this instance, is in the global namespace. The outer function(local )’s namespace contains the variable hisMarks
, and the inner function’s nested local namespace contains the variable engMarks()
.
mathMarks
is global, hisMarks
is nonlocal, and engMarks
is local to us when we are in inner function(). We can read from the inner function and add new values to engMarks
, but we can only read from it engMarks
and mathMarks()
.
A new variable name hisMarks
is created in the local namespace when we attempt to assign the mathMarks
value to the nonlocal variable engMarks
. When we give a value, the same thing occurs.
However, we receive all references and assignments if we designate them as global. Similarly, the variable hisMarks
must be marked as nonlocal if we want to rebind it. The below given an example will make this clear.
Example
def outer_function(): mathMarks = 80 def inner_function(): mathMarks = 90 print('mathMarks =', mathMarks) inner_function() print('mathMarks =', mathMarks) mathMarks = 100 outer_function() print('mathMarks =', mathMarks)
Output
mathMarks = 90 mathMarks = 80 mathMarks = 100
This concludes the Python Namespace lesson. In the next lesson, you will learn about IO and import in Python and their usage.