In This Article
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 what they are. How they work in Python. And also, how they can be used to organize our code and avoid making mistakes.
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.
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.
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.
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.
But, 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.
# global variable x = 10 def my_function(): # local variable x = 5 print(x) print(x) my_function() print(x)
The output to this will be:
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
y = 10 def my_function(): global y y = 5 print(y) print(y) my_function() print(y)
The output to this will be:
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.
Namespaces and the scope of variables in Python are important parts of its programming language.
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.