Page Contents
In this tutorial we will learn about Namespace and Scope in Python . Whenever we create any object in Python a new name same as the object name is stored in the Python namespace . Scope is that part of the program from which these names are accessible .
Namespace in Python
Namespace in Python is a method by which different objects in python are given a different name and with this name any object is identified later . There are three types of namespaces in python and they are as follows.
- Built-in Namespace in Python
- Global Namespace in Python
- Local Namespace in Python
Now we will know about all these namespaces in details –
Built-in Namespace in Python
Built-in namespace in Python contains names of all built-in objects and they are always available when a Python program is running . Using the following command we can know the name of all built-in objects .
print(dir(__builtins__))
['ArithmeticError', 'AssertionError', 'AttributeError',.....,'super', 'tuple', 'type', 'vars', 'zip']
Global Namespace in Python
All the names defined in the main program of python are included in the global namespace. These names exist until the program is terminated. Also, if we import a module, the names of that module are also included in the global namespace.
Local Namespace in Python
When we define a function in python, all the names that are defined in that function are included in the local namespace. These names are available only during the function calling .These names do not exist after function calling .
Namespace in Python example
Example 1:
# defining a function
def add(a, b):
return a + b
x = add(5, 6)
print(x)
11
In the above code, first a function named add
has been created, which has two variables a
, b
. Then the result of the add()
function is stored in a variable named x
, then x
is printed. A list below shows which name belongs to which namespace –
print
belongs to the built-in namespaces. Because it is always available during the program.- The names used in the main program, such as
add
,x
belong to global namespaces. - Names used in
add()
function such asa
,b
belong to local namespaces.
Scope of an Object in Python
Scope is the area of a program from which a specific object can be accessed. We cannot access any object from any part of the program. In python, whenever we try to access an object, python first searches the local namespaces to see if the object exists, if not, then to the global namespaces. If not found in global namespaces, python searches for that object in the built-in namespace.
Scope in Python Examples
Example 1:
x = 20
# defining a function
def change():
x = 10
print(x)
# calling the function
change()
print(x)
10 20
In the above code, first we have written x = 20
, that is, an object named x
is created in the global namespace with a value of 20
. Then we have written a function called change
where we have written x = 10
, that is, an object named x
is created in the local namespace (since it is written in the function) whose value is 10
. So we now have two objects named x
, one with a value of 20
in the global namespace and the other with a value of 10
in the local namespace.
Now we have called the change()
function. When program faces print(x)
statement inside the function it will search for x
in local namespace . Since there is an object named x
in the local namespace with value 10
, program will print 10 in the output .
After function calling, program will again face print(x)
statement. As this print(x)
statement is written outside the function so it will search for x
in global namespace . Since there is an object named x
in the global namespace with value 20
, program will print 20
in the output .
Example 2:
x=20
# defining a function
def change():
print(x)
# calling the function
change()
print(x)
20 20
In the above code, first we have written x = 20
, that is, an object named x
is created in the global namespace with a value of 20
. Then we wrote a function called change
where we did not create any object named x
. So we have only one object named x
which is in global namespace and its value is 20
.
Now we have called the change()
function. When program faces print(x)
statement inside the function it will search for x
in local namespace . Since there is no object named x
in the local namespace, python will look for an object named x
in the global namespace. Since there is an object named x
in the global namespace with value 20
, program will print 20
in the output .
After function calling, program will again face print(x)
statement. As this print(x)
statement is written outside the function so it will search for x
in global namespace . Since there is an object named x
in the global namespace with value 20
, program will print 20
in the output .
Example 3:
# defining a function
def change():
x = 10
print(x)
# calling the function
change()
print(x)
10
--------------------------------------------------------------------------- NameError Traceback (most recent call last) C:\Users\RAJKUM~1\AppData\Local\Temp/ipykernel_1740/3558322978.py in <module> 6 # calling the function 7 change() ----> 8 print(x) NameError: name 'x' is not defined
In the above code we have written a function called change where we have written x = 10
, that is, an object named x
has been created in the local namespace(since it is written in the function) whose value is 10
, . So we have only one object named x
, which is in the local namespace and whose value is 10
.
Now we have called the change()
function. When program faces print(x)
statement inside the function it will search for x
in local namespace . Since there is an object named x
in the local namespace with value 10
, program will print 10
in the output .
After function calling, python will again face print(x)
statement. Since it is written outside the function, python will look for an object named x
outside the function i.e. in the global namespace. Since there is no object named x
in global namespace, it will show NameError
.
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .