Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Python Functions 4 Namespace

The document explains namespaces in Python, which are systems to manage names in a program and ensure uniqueness to avoid conflicts. It details three types of namespaces: built-in, global, and local, along with the LEGB rule for name resolution. Additionally, it provides examples illustrating how namespaces work in various scenarios involving function calls and variable scopes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Python Functions 4 Namespace

The document explains namespaces in Python, which are systems to manage names in a program and ensure uniqueness to avoid conflicts. It details three types of namespaces: built-in, global, and local, along with the LEGB rule for name resolution. Additionally, it provides examples illustrating how namespaces work in various scenarios involving function calls and variable scopes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PYTHON FUNCTIONS – Part 4

Namespaces and Name resolution in Python

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.

Namespace is a mapping of every name in a Python program to a corresponding object.


Python implements namespaces in the form of dictionaries, where names act as keys, and
the objects as values.
There are three types of namespaces in Python:
1. Built-in namespace : Whenever we start the Python interpreter, a built-in
namespace is created, which will exist as long as Python is running. It holds all the
built-in names of Python like type(), id(), print(), max(), etc. Hence we can directly
access these functions.
Eg. x=10
print(type(x))

2. Global namespace : A global namespace is created when we start running a Python


program. It contains all the names defined in the program. Each module that is
imported also creates its own global namespace. It holds all the names existing in
that module. For eg. if math module is imported, all the functions in it are held in
the math namespace, and you can access it through the name, like math.sqrt(),
math.pow(), etc
Eg. import math
print(math.sqrt(25))

3. Local namespace : Whenever a function is called, a local namespace is created to


hold all the names in that function. And such names can be accessed only within
that namespace. Also, if we use from..import.. statement to import a particular
function from a module, then that function is added to the local namespace, and
then we can directly access it without specifying the module name.
Eg. from math import sqrt
print(sqrt(25))

Note : If a function definition contains another function definition, or invokes another


function, then the namespace for that function can be considered as an Enclosing
namespace.
The following diagram clarifies the concept of different namespaces.

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

b 4 Enclosing Namespace for area()

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

Local Namespace for area()


b 4
s 4

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

The global variable x still refers to the value 25


Example 4
Finally, let’s see an example where a global variable is made accessible inside a function, by
specifying the global keyword.

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

The global variable x is now referring to the value 10.

You might also like