Skip to main content

Python: Namespace and Scope

1. Namespace

A namespace is a system that has a unique name for each and every object in Python. It is a collection of currently defined symbolic names along with information about the object that each name references.

Python itself maintains a namespace in the form of a Python dictionary. Name is simply a name given to objects. Everything in Python is an object. Name is a way to access the underlying object.

A lifetime of a namespace depends upon the scope of objects, if the scope of an object ends, the lifetime of that namespace comes to an end. It creates namespaces as necessary and deletes them when they’re no longer needed.

  1. The Built-In Namespace: It contains the names of all of Python’s built-in objects. These are available at all times when Python is running.
  2. The Global Namespace: It contains any defined name at the level of the main program. Python creates the global namespace when the main program body starts, and it remains in existence until the interpreter terminates.
  3. The Local and Namespaces: The interpreter creates a new namespace whenever a function executes. That namespace is local to the function and remains in existence until the function terminate.

Example:

a = 5
print('id(a) =', id(a))

b = a + 1
print('id(b) =', id(b))
print('id(6) =', id(6))

c = 5
print('id(c) =', id(c))
print('id(5) =', id(5))

Output:

id(a) = 140729565325232
id(b) = 140729565325264
id(6) = 140729565325264
id(c) = 140729565325232
id(5) = 140729565325232

An object 5 is created and the name a is assigned with it, when we do b=a+1, a new object 6 is created and now b is assigned with this object.

Note: That id(b)and id(6) have the same values.

Furthermore, when c=5 is executed, the new name c gets assigned with the previous object 5.

2. Scope

It is the portion of a program from where a namespace can be accessed directly without any prefix. The scope will depend on the coding region where the variable or object is located.

It refers to the coding region from where a particular Python object is accessible. Hence one cannot access any particular object from anywhere from the code, the accessing has to be allowed by the scope of the object.

The scope of a name is the region of a program in which that name has meaning. The interpreter determines this at runtime based on where the name definition occurs and where in the code the name is referenced.

Example

a = 50 #global namescpace

def func_1():
    a=10 #local namespace
    print("a=",a)
    def func_2():
        a=30 #nested local namespace
        print("a=",a)
    func_2()
    
func_1()
print("a=", a)

Output

a= 10
a= 30
a= 50

In this program, three different variables are defined in separate namespaces and accessed accordingly.

Tags
Submitted by devanshi.srivastava on June 29, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.