Namespacing and Scopes in Python

Namespacing and Scopes in Python

Namespacing and Scopes in Python

You do remember that everything in Python is an object, right? Well, how does Python know what you are trying to access? Think of a situation where you have 2 functions with the same name. You would still be able to call the function you need. How is that possible? This is where namespacing comes to the rescue. Lets see what is Namespacing and Scopes in Python.

Namespacing is the system that Python uses to assign unique names to all the objects in our code. And if you are wondering, objects can be variables and methods. Python does namespacing by maintaining a dictionary structure. Where names act as the keys and the object or variable acts as the values in the structure.

Well, a name is just a way that you use to access the objects. These names act as a reference to access the values that you assign to them.

Example: a=5, b=’Iceland!’

If I would want to access the value ‘Icekand!’ I would simply call the variable name by ‘b’ and I would have access to ‘Iceland!’. These are names. You can even assign methods names and call them accordingly.

1
2
3
import math
square_root = math.sqrt
print('The square root is ',square_root(9))

Output: The root is 3.0

Namespacing works with scopes. Scopes are the validity of a function/variable/value inside the function or class they belong to. Python built-in functions namespacing covers all the other scopes of Python. Functions such as print() and id() etc. can be used even without any imports and be used anywhere. Below them is the global and local namespacing. Let me explain the scope and namespacing in a code snippet below :

1
2
3
4
5
6
7
8
9
10
def add():
    x = 3
    y = 2
    def add2():
        p, q, r = 3, 4, 5
        print('Inside add2 printing sum of 3 numbers:'(p+q+r))
    add2()
    print('The values of p, q, r are :', p, q, r)
    print('Inside the add printing sum of 2 numbers:'(x+y))
add()

As you can see with the code above, I have declared 2 functions with the name add() and add2(). You have the definition of the add() and you later call the method add(). Here in add() you call add2() and so you are able to get the output of 12 since 3+4+5 is 12. But as soon as you come out of add2(), the scope of p,q,r is terminated meaning that p,q,r are only accessible and available if you are in add2(). Since you are now in add(), there is no p,q,r and hence you get the error and execution stops.

You can get a better understanding of the scopes and namespacing from the figure below. The built-in scope covers all of Python making them available whenever needed. The global scope covers all of the programs that are being executed. The local scope covers all of the methods being executed in a program. That is basically what namespacing is in Python. Let us move ahead with flow control in Python Basics.

namespaces in python

 

Related Posts:

Python Tutorial – Learn Python

What is Python? What makes Python so Powerful?

Variables in Python – Constant, Global & Static Variables

Operators in Python

Data Types in Python

STRING Data Type in Python

LIST Data Structure in PYTHON

TUPLE Data Structure in PYTHON

Differences between List and Tuple

SET Data Structure in PYTHON

DICTIONARY Data Structure in PYTHON

Database Programming in Python

What is Multithreading in Python?

Python Exception Handling Using try, except and finally statement

File Handling in Python

Python Random module

Python reduce() Function

Python map() Function

Python filter() Function

Lambda Function in PYTHON (Anonymous Function)

Python Interview Question And Answers