Python Namespaces

A namespace represents a memory block where names are mapped (or linked) to objects. Suppose we write:

n = 10

Here, 'n' is the name given to the integer object 10. Please recollect that numbers, strings, lists etc. are all considered as objects in Python. The name 'n' is linked to 10 in the namespace.

A class maintains its own namespace, called 'class namespace'. In the class namespace, the names are mapped to class variables. Similarly, every instance will have its own name space, called 'instance namespace'. In the instance namespace, the names are mapped to instance variables.

example image for python namespace

class Student:
n=10

print(Student.n)


Student.n+=1


print(Student.n)


example image for python namespace instance

Modifying the class variable in the instance namespace