L9_M1_Namespaces and Scope in Python
L9_M1_Namespaces and Scope in Python
Technology
Initially, an object 2 is created and the name a is associated with it, when we do a
= a+1, a new object 3 is created and now a is associated with this object.
Furthermore, when b = 2 is executed, the new name b gets associated with the
previous object 2.
Amity School of Engineering &
Technology
This is efficient as Python does not have to create a new duplicate object. This
dynamic nature of name binding makes Python powerful; a name could refer to
any type of object.
>>> a = 5
>>> a = 'Hello World!'
>>> a = [1,2,3]
All these are valid and a will refer to three different types of objects in different
instances.
Amity School of Engineering &
Technology
What is a Namespace in Python?
There are three types of Python namespaces- global, local, and built-in. It’s
the same with a variable scope in python. Also, the ‘global’ keyword lets us
refer to a name in a global scope. Likewise, the ‘nonlocal’ keyword lets us
refer to a name in a nonlocal scope.
Amity School of Engineering &
Technology