In this tutorial, we are going to discuss some technical terms of python that we often use in python and other programming languages. so let’s discuss Python Name, Namespace, and Scope.
Python Name, Namespace, and Scope
Name in Python
Name in python also known as Identifiers. As we have discussed earlier that everything in python is an object. Identifiers are the names which are used assign to a data or object.
If we specifically, discuss what is Name in python, we can say that Name is a name that can be assigned to an object, and the Name helps us to access the object.
Let’s understand it with an Example
Suppose, you used assignment operator (=) to assign a value 200 which is an integer or you can also call it an object and you assign the value 200 to a variable number. So, here 200 is the object and number is Name.
Let’s code the above statement using python
number = 200 print(number)
Let’s understand the concept of Name with the inbuilt function id()
id() function is used to return the memory location of the object in the integer form.
number_1 = 200 number_2 = 200 print("the ID of number is: ", id(number_1)) print("the ID of 200 is: ",id(200)) print("the ID of number_2 is: ", id(number_2))
#the ID values might be different when you run the above program on your system. the ID of number is: 1770734960 the ID of 200 is: 1770734960 the ID of number_2 is: 1770734960
from the above example, you can see that the id values for each variable and even for the object (200) is the same what does it signify? It signifies that every object occupies a unique place in the memory and the Names or identifies which are associated with that object are just used to access it.
Using the Name concept of Python you can make duplicates of a predefined function, for example, you can duplicate the function print()
Let’s code to duplicate the function print().
dup_print = print dup_print("this statement is printing using Duplicate print")
this statement is printing using Duplicate print
You cannot duplicate the name; a name cannot access the two different objects simultaneously. A Name can access to a single object.
var = "Hello" var = 23 var = [1,2,3,4,5] print(var)
[1, 2, 3, 4, 5]
Namespace in Python
As in the above example, we have discussed that a Name cannot be duplicated or a name can only access to an object. In simple words, two objects cannot share the same Name. For example, a variable cannot have two objects as its value.
But what if we want a variable name common for two different objects can we do that? Yes, with the help of Namespace concept we can name two different objects by the same name and use the different objects for different purposes.
So, if we define the Namespace; Namespace is a concept in python which allows different objects to share a common name without having any conflict between them.
For using the namespace concept there is a condition that both the name having different objects or structure should be isolated from each other.
Let’s understand it with an example:
var = 30 #var outside the function def name_space(): var= [1,2,3,4,5] # var inside a function print(var) name_space() print(var)
[1, 2, 3, 4, 5] 30
In the above example, we seen that var has two objects one is integer 30 and other is a list [1,2,3,4,5], but both the name var are separated from each other because we have isolated the list var inside the function name_space(). You can see that the python has no problem with the var which is outside the function and the value of var which is outside the function did not change either.
The namespace is divided into 3 types:
- Built-in Namespace
- Global Namespace
- Local Namespace
Python Variable Scope
The scope is a concept in python which deals with the visibility of the identifiers or names, it is basically used to tell the programme which part of the programme can access which variable. The scope comes very usefully when we use the concept of Namespace.
Python has 2 scopes.
- Local scope
- Global Scope.
Local Scope variable: All those variables which are assigned inside a function known as local scope Variable
Global Scope variable: All those variables which are outside the function termed as a global variable
Let’s understand it with an example;
global_var = 30 # global scope def scope(): local_var = 40 #local scope print(global_var) print(local_var) scope() print(global_var)
30 40 3
In the above example, you saw that we can access a global variable inside a function but can we access the local variable outside the function no. if you do that it will throw an error.
def scope(): local_var = 40 print(local_var) scope() print(local_var)
Traceback (most recent call last):
File "importt.py", line 9, in <module> print(local_var) NameError: name 'local_var' is not defined
In the above example we saw that we can access the global variable inside a function can we change alter its value inside the function?
Yes, for that you need a keyword global which will import the global variable inside the function and you can alter its value which will remain permanent.
Let’s understand it with an example.
global_var = 30 def scope(): global global_var global_var+=70 scope() print(global_var)
What if you do not use the global keyword to import the global variable inside the function then it will throw an error.
Points to remember;
- The name is the identifier
- Two objects can share the same name if both are isolated.
- You can not access the local variable outside the function
- To import the global variable instead of making a namespace inside a function use the global keyword