What is an Identifier in Python?
An identifier is a name given to objects and is used to access the object to which it refers.
For example:
a = 2
print(id(a)) # 9789024
a = a + 1
print(id(a)) # 9789056
print(id(3)) # 9789056
b = 2
print(id(b)) # 9789024
print(id(2)) # 9789024In the picture is shown the memory diagram of the variables involved in the previous example:

Explanation:
- Initially an object
2is created with the nameaassociated with it; - When
a = a+1is executed, a new object3is created andais associated with this object. Nowid(a)andid(3)have the same values. - When
b = 2is created, the new namebis associated with the previous object2.
What is a Namespace in Python?
A namespace is a collection of names.
It is a system that provides a unique name for each object (such as variables and methods). A namespace is saved in the form of a Python dictionary.
Types of namespaces
In a Python, there are three types of namespaces:
- Built-In namespace: it is created when we start the Python interpreter and exists as long as the interpreter runs.
- Global namespace: each module creates its own global namespace
- Local namespace: it is created when a function is called, which has all the names defined in it

Python Variable Scope
A scope is the portion of a program from where a namespace can be accessed directly without any prefix.
At any given moment, there are at least three nested scopes:
- Scope of the current function that has local names.
- Scope of the module that has global names.
- Outermost scope that has built-in names.
For example:
var1 = 1
def a_function():
var2 = 2
def an_inner_function():
var3 = 3Examples
Scope Example
Thanks to isolation between namespaces, there may be multiple objects with the same name present in different namespaces.
Consider the following example:
a = 10
def outer_function():
b = 20
def inner_func():
c = 30The following observations can be made:
- The variable
ais in the global namespace. - The variable
bis in the local namespace ofouter_function(). - The variable
cis in the nested local namespace ofinner_function(). - When you are in
inner_function(),cis local to you,bis nonlocal andais global. You can read and assign new values tocand you can only readaandbfrominner_function() - Trying to assign a value to variable
binvolves the creation of a new variablebthat is different that nonlocalbvariable. The same thing happens when you try to assign a value to variablea
Global Example
If a is declared as a global variable, all references and assignments are made to the global variable a.
Consider this example:
def outer_function():
a = 2
def inner_function():
a = 3
print('inner a =', a)
inner_function()
print('outer a =', a)
a = 1
outer_function()
print('global a =', a)Output
inner a = 3
outer a = 2
global a = 1def outer_function():
global a
a = 2
def inner_function():
global a
a = 3
print('inner a =', a)
inner_function()
print('outer a =', a)
a = 1
outer_function()
print('global a =', a)Output
inner a = 3
outer a = 3
global a = 3