In this tutorial, we will learn Python Namespace and Scope. It is a basic fundamental concept of Python that is required for understanding the workflow of a Python program.
Python Namespace and Scope
A namespace in Python is a type of mapping of names to Python objects. Multiple python namespaces can exist together without any collision with Python Namespace.
Multiple namespaces in python can have the same name without worrying about any conflicts.
- A python namespace is created at the start of a Python Interpreter.
- A Python dictionary is maintained for the objects in a namespace.
- Modules have a global namespace.
- A local python namespace is created whenever a function is called. The namespace is created for all the names available in the function.
- Built-in namespaces are always present due to which we have an id() and a print() function.
Types of Python Namespace
There are 3 types of namespace available in Python. They are:
- Built-In Namespace
- Global Namespace
- Local Namespace
The built-in namespace has Python’s all the built-in objects. This namespace is available all the time whenever a Python program is being executed. Most of today’s Python developers are unaware of how namespaces work and lack such concepts. To see the list of all the objects that are available all the time, run this command in the Python terminal.
The above command will display a list of all the objects that come built-in whenever a python interpreter is started. These objects have a lifetime till the interpreter is stopped.
The names that are defined at the level of the main program come under the global namespace. The global namespace is created whenever the main body of the program starts executing. The lifetime of this namespace is till the interpreter is stopped.
Along with this, the modules that are loaded with the import keyword during the runtime of a Python program are also contained under the global namespace.
At an instance, to see all the objects that come under the global namespace can be seen using the following command in a python terminal:
The above command returns a Python dictionary that consists of all the objects of the global namespace at that time.
A Local namespace is created whenever a function is executed in Python. The Python interpreter creates a new namespace whenever a function is called. The newly created namespace is local to the function itself. The lifetime of the namespace is till the function is terminated.
At an instance, to see all the objects that come under the local namespace can be seen using the following command in a python terminal:
The above command returns a Python dictionary that consists of all the objects of the local namespace at that instance of time.
What is Python Scope?
A Python scope is a region in Python code. Each region may have any number of variables and functions but their names should be different. There can be multiple scopes in a Python program. A name can coexist in multiple local scopes of a Python program.
Let’s learn more about scopes using few examples.
The example below has a variable name that is defined only once in the program. Later, the same variable is being printed out in the terminal. As we see, there exists a function function1 that contains another function function2 and the function2 function is the one that prints the variable name.
name = 'UseMyNotes' def function1(): def function2(): print(name) function2() function1() #output #UseMyNotes
A variable in a higher scope and be manipulated from a lower scope region of a program.
name = 'UseMyNotes' def function1(): name = 'Welcome to UseMyNotes' def function2(): print(name) function2() function1() # output # This is globally available
In the above code, the global scoped variable name is being manipulated with the local scope of a function. As this function is under the scope of the name variable, it can access or manipulate them directly.
The name variable is manipulated locally in a function and then printed to the terminal in the next scenario.
name = 'Main Global' def function1(): name = 'This is UseMyNotes' def function2(): name = "Local area" print(name) function2() function1() # output # Local area
With the above code, it is clear that a function uses the locally scoped variables first if they are in that scope. If not, then the next higher scope variables are accessed next.
This is all about Python Namespace and Scope. I hope now you have a better understanding of namespaces and scopes in Python. To learn more on Python, keep following us.