Python Functions 4 Namespace
Python Functions 4 Namespace
Namespaces in Python
Everything in Python is considered as an object. A name is used to access a Python object,
like a value or a function. A namespace is a simple system to control the names in a
program. They are used to distinguish between different sections of a Python program. It
ensures that all names in a namespace are unique. It is a practical approach to define the
scope of a name and to avoid name conflicts within a program.
Name Resolution
A variable in a Python program is associated with its name. Whenever you try to access a
variable, Python follows a name resolution rule known as the LEGB rule. It is used to resolve
the scope of a variable.
Python does the following steps for every name reference in a program:
Step 1 (L) : It checks within the Local namespace, whether the variable exists. If it
does, it uses it, otherwise, it moves to the next step.
Step 2 (E) : It checks within the Enclosed namespace, whether the variable exists.
If it does, it uses it, otherwise, it moves to the next step.
Step 3 (G) : It checks within the Global namespace, whether the variable exists. If
it does, it uses it, otherwise, it moves to the next step.
Step 4 (B) : It checks within the Built-in namespace, whether the variable exists. If
it does, it uses it, otherwise, it generates the following error :
NameError: name '<variable>' is not defined
Namespace Examples
Now let us see different examples how namespaces are created whenever a variable is
defined, and how they are accessed, when a Python program is executed.
Example 1
Let’s look at a program code, where we have a function call.
1. def change(x,y): #function definition
2. x=x+10
3. ` y=y-5
4. print(x,y)
5. a,b = 22,35
6. change(a,b) #function invoking
7. print(a,b)
The flow of execution starts from 1 -> 5. When it comes to Line 5, this is the scenario.
Global Namespace
a 22
b 35
Then the flow of execution moves to 6 -> 1. When it comes to Line 1, this is the scenario.
Global Namespace
a 22 Local Namespace
x 22
b 35
y 35
Then the flow of execution moves to 2 -> 3. When it comes to Line 3, this is the scenario.
Global Namespace
a 22 Local Namespace
x 22 32
b 35
y 35 30
Finally the flow of execution moves to 4 -> 7. When it comes to Line 7, this is the scenario.
Global Namespace
a 22
b 35
Example 2
Let’s look at another program code, where we have a function calling another function :
1. def power(x,n): #function definition
2. return x**n
3.
4. def area(s): #function definition
5. a=power(s,2) #function call
6. print('Area of the square is', a)
7.
8. b=4
9. area(b) #function call
10. print('Done')
The flow of execution starts from 1 -> 4 -> 8. When it comes to Line 8, this is the scenario.
Global Namespace
b 4
Then the flow of execution moves to 9 -> 4. When it comes to Line 4, this is the scenario.
Global Namespace
Local Namespace for area()
b 4
s 4
Then the flow of execution moves to 5 -> 1. When it comes to Line 1, this is the scenario.
Global Namespace
s 4
Local Namespace for power()
x 4
n 2
Then the flow of execution moves to 2 -> 5. When it comes to Line 5, this is the scenario.
Global Namespace
a 16
Finally the flow of execution moves to 6 -> 10. When it comes to Line 10, this is the
scenario.
Global Namespace
b 4
Example 3
Now, lets see an example where a local variable and a global variable have the same name.
1. def alter():
2. x=10
3. print('Inside function..')
4. print('x =',x) #accessing local x
5. x = 25
6. print('Inside main program..')
7. print('x =',x) #accessing global x
8. alter()
9. print('Inside main program..')
10. print('x =',x) #accessing global x
The flow of execution starts from 1 -> 5. When it comes to Line 5, this is the scenario.
Global Namespace
x 25
Then the flow of execution moves to 6 -> 7 -> 8 -> 1 -> 2. When it comes to Line 2, this is the
scenario.
Global Namespace
Local Namespace for alter()
x 25
x 10
Finally the flow of execution moves to 3 -> 4 -> 9 -> 10. When it comes to Line 9, this is the
scenario.
Global Namespace
x 25
1. def alter():
2. global x #declaring x as global
3. x=10
4. print('Inside function..')
5. print('x =',x) #accessing global x
6. x = 25
7. print('Inside main program..')
8. print('x =',x) #accessing global x
9. alter()
10. print('Inside main program..')
11. print('x =',x) #accessing global x
The flow of execution starts from 1 -> 6. When it comes to Line 6, this is the scenario.
Global Namespace
x 25
Then the flow of execution moves to 7 -> 8 -> 9 -> 1 -> 2 -> 3. When it comes to Line 3, this
is the scenario.
Global Namespace
Local Namespace for alter()
x 25 10
Finally the flow of execution moves to 4 -> 5 -> 10 -> 11. When it comes to Line 10, this is
the scenario.
Global Namespace
x 10