In this tutorial, you will learn about the namespace, mapping from names to objects, and the scope of a variable.
In this article, you will learn-
What is Name in Python?
If you have ever read ‘The Zen of Python’ (type import this in the Python interpreter), the last line states, Namespaces are one honking good idea – let’s do more of those! So what are these baffling namespaces? Let our first look at what name is.
Name (also called identifier) is just a name given to objects. Everything in Python is on an object. A name is an approach to access to the underlying object.
For instance, when we do the task a = 2, 2 is an item put away in memory and an is the name we partner it with. We can get the address (in RAM) of some articles through the built function id(). Let’s look at how to use it.
# Note: You may get different values for the id a = 2 print('id(2) =', id(2)) print('id(a) =', id(a))
id(2) = 9302208 id(a) = 9302208
Here, both refer to the same object 2, so they have the same id(). Let’s make things a little more interesting.
# Note: You may get different values for the id a = 2 print('id(a) =', id(a)) a = a+1 print('id(a) =', id(a)) print('id(3) =', id(3)) b = 2 print('id(b) =', id(b)) print('id(2) =', id(2))
id(a) = 9302208 id(a) = 9302240 id(3) = 9302240 id(b) = 9302208 id(2) = 9302208
What is happening in the above sequence of steps? Let’s use a diagram to explain this:
Memory diagram of variables in Python
Initially, an object
2 is created and the name a is associated with it when we do
a = a+1, a new object
3 is created and now is associated with this object.
id(3) have the same values.
b = 2 is executed, the new name b gets associated with the previous object
This is efficient as Python does not have to create a new duplicate object. This dynamic nature of name binding makes Python powerful; a name could refer to any type of object.
>>> a = 5 >>> a = 'Hello World!' >>> a = [1,2,3]
All these are valid and will refer to three different types of objects in different instances. Functions are objects too, so a name can refer to them as well.
def printHello(): print("Hello") a = printHello a()
The same name can refer to a function and we can call the function using this name.
What is a Namespace in Python?
Now that we understand what names are, we can proceed onward to the idea of namespaces.
To just put it, a namespace is an assortment of names.
In Python, you can envision a namespace as a mapping of each name you have characterized to relating objects.
Diverse namespaces can coincide at a given time yet are totally segregated.
A namespace containing all the implicit names is made when we start the Python translator and exists as long as the mediator runs.
This is the explanation that implicit capacities like id(), print(), and so forth are consistently accessible to us from any piece of the program. Every module makes its own worldwide namespace.
These distinctive namespaces are segregated. Subsequently, a similar name that may exist in various modules doesn’t impact.
Modules can have various functions and classes. A local namespace is created when a function is called, which has all the names defined in it. Similar is the case with class.
Python Variable Scope
Although there are various unique namespaces defined, we may not be able to access all of them from every part of the program.
An extension is the segment of a program from where a namespace can be gotten to legitimately with no prefix.
At any given moment, there are at least three nested scopes.
Scope of the current function which has local names
Scope of the module which has global names
The outermost scope which has built-in names
When a reference is made inside a function, the name is looked in the nearby namespace, at that point in the worldwide namespace lastly in the implicit namespace.
On the off chance that there is a capacity inside another capacity, another degree is settled inside the local scope.
Example of Scope and Namespace in Python
def outer_function(): b = 20 def inner_func(): c = 30 a = 10
Here, the variable a is in the global namespace. Variable b is in the local namespace of
outer_function() and c is in the nested local namespace of
When we are in
inner_function(), c is local to us, b is nonlocal and a is global. We can read as well as assign new values to c but can only read b and a from
If we try to assign as a value to b, a new variable b is created in the local namespace which is different than the nonlocal b. The same thing happens when we assign a value to a.
However, if we declare a global, all the reference and assignment go to the global a. Similarly, if we want to rebind the variable b, it must be declared as nonlocal. The following example will further clarify this.
def outer_function(): a = 20 def inner_function(): a = 30 print('a =', a) inner_function() print('a =', a) a = 10 outer_function() print('a =', a)
As you can see, the output of this program is
a = 30 a = 20 a = 10
In this program, three different variables a are defined in separate namespaces and accessed accordingly. While in the following program,
def outer_function(): global a a = 20 def inner_function(): global a a = 30 print('a =', a) inner_function() print('a =', a) a = 10 outer_function() print('a =', a)
The output of the program is.
a = 30 a = 30 a = 30
Here, all references and assignments are to the global a due to the use of keyword
Please feel free to give your comment if you face any difficulty here.
For More Latest Articles Click on Below Link