Tutorial

Namespace and Scope of Variables in Python

3 min read

Just like in any other programming language, namespace and the scope of variables in Python are very important concepts.

In this article, we’ll take a closer look at the namespace and scope of variables in Python. How they work and also, how they can be used to organize our code and avoid making mistakes.

Python Namespaces

In Python, a namespace is a collection of names (also known as identifiers) and their corresponding values.

Each name in a namespace corresponds to a specific object in memory.

Namespaces are used to organize and keep track of the different variables, functions, and classes that are defined in a program.

There are different types of namespaces in Python, including the global namespace, local namespace, and built-in namespace.

  1. Global Namespace – It is the top-level namespace that contains all the names that are defined at the top level of a script or module. The global namespace is shared by all functions and classes defined in the script or module.
  2. Local Namespace – It is the namespace that is created and used within a function. Local names are only accessible inside the function in which they are defined.
  3. Built-in Namespace – It contains the names of all the built-in functions and classes that are provided by the Python standard library. These names are automatically available in all scripts and modules. We just need to import them while writing our code.

Scope of Variables in Python

It refers to the part of a program where a name (or identifier) can be accessed.

We can also say that it defines the visibility and accessibility of names in a program.

In Python, the scope of a name is determined by the location of its definition.

Names that are defined in the global namespace have global scope and are accessible from anywhere in the script or module.

However, names that are defined inside a function or class have local scope and are only accessible inside the function or class.

When a name is called in a program, Python will first search for the name in the local namespace. If the name is not found, Python will then search in the global namespace. If the name is still not found, then Python will check the built-in namespace. This process is known as the Name Resolution Order.

Example:

# global variable
x = 10

def my_function():
    # local variable
    x = 5
    print(x)

print(x)
my_function()
print(x)

The output of this will be:
10
5
10

Here, we have defined x first as a global variable and then inside the function. Note: We have defined it as a local variable with a different value.

When we call my_function() it will use the local variable defined inside the function and it will be accessible only inside the function, whereas the global variable will be accessible from anywhere.

Global Keyword in Python

Example of global keyword in Python

y = 10

def my_function():
    global y
    y = 5
    print(y)

print(y)
my_function()
print(y)

The output of this will be:
10
5
5

Here, we use the global keyword before the variable name. We are saying that we want to use the global variable inside the function and any changes that are made inside the function will also be reflected in the global variable.

Conclusion

Namespaces and the scope of variables in Python are important.

Understanding their work in Python can help us write more organized and maintainable code.

Moreover, by using namespaces and scope correctly, we can avoid naming conflicts and make sure that our variables are accessible from the required sections of our program.

Hope you liked the tutorial, You can get a Python online tutor for yourself here at Tutor Python.