Skip to content

Python Namespace and Scope

How Python determines whether a variable like var1 belongs to the global namespace and how scope rules affect variable access and modification.

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)) # 9789024

In the picture is shown the memory diagram of the variables involved in the previous example: Memory diagram of variables of the example

Explanation:

  1. Initially an object 2 is created with the name a associated with it;
  2. When a = a+1 is executed, a new object 3 is created and a is associated with this object. Now id(a) and id(3) have the same values.
  3. When b = 2 is created, the new name b is associated with the previous object 2.

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:

  1. Built-In namespace: it is created when we start the Python interpreter and exists as long as the interpreter runs.
  2. Global namespace: each module creates its own global namespace
  3. Local namespace: it is created when a function is called, which has all the names defined in it

Diagram of different namespaces in Python

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:

  1. Scope of the current function that has local names.
  2. Scope of the module that has global names.
  3. Outermost scope that has built-in names.

For example:

var1 = 1
def a_function():

    var2 = 2
    def an_inner_function():
    
        var3 = 3

Examples

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 = 30

The following observations can be made:

  • The variable a is in the global namespace.
  • The variable b is in the local namespace of outer_function().
  • The variable c is in the nested local namespace of inner_function().
  • When you are in inner_function(), c is local to you, b is nonlocal and a is global. You can read and assign new values to c and you can only read a and b from inner_function()
  • Trying to assign a value to variable b involves the creation of a new variable b that is different that nonlocal b variable. The same thing happens when you try to assign a value to variable a

Global Example

If a is declared as a global variable, all references and assignments are made to the global variable a.

Consider this example:

Without Globalpython
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 = 1
With Globalpython
def 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
Navigation