Namespaces and Scopes help us to understand variable hierarchy. If we don’t use these concepts, the variables can be modified during execution or we might try to access them out of scope, which is problematic.
NameSpace in Python –
A namespace can give priority to the variables having the same names and scope can help to identify the lifetime of the variable during the program execution.
A namespace can be considered as mapping objects to some names or a collection of some names.
Whenever we start a python interpreter, a namespace containing all the built-in functions used in basic Python code is created and exists till the end of the execution of the code.
There are 3 different types of namespaces –
- Built-in Namespace
- Global Namespace
- Local Namespace
Let’s discuss them all –
- Built-In Namespace –
It contains predefined functions and objects for use in code. Their lifetime is till the termination of the program. When a python interpreter runs, some functions are provided already like print(), id(), etc. so these are the in-built python functions that we can use without using any namespace, etc.
Example – list, int, input, enumerate, etc.
- Global Namespace –
It consists of all the variables which we define at the main. if we are using a python module from another file (and it contains some global variables), then they are also used as it is.
a = 5 # global variable print(a) # will print 5 , by taking the value from a global variable.
We can also use the global variables of one module in another. This happens because of the concept of Global Namespace.
- Local Namespace –
when a local function is called or invoked, then a local namespace is created. Objects and variables declared inside this scope, can not be used outside of the program block.
def add(): a = 10 b = 10 print(a+b) add() #print(a)
#print(a) #this line won’t print anything and will actually give an error because we are trying to access the value of an outside the scope of the add function, in which it is declared.
A Scope is a region, in which some variables or functions are accessible, or a scope can be considered as the lifetime of a namespace. It decides whether we can access a particular variable or not. A variable having a global scope can be accessed throughout the program, till the termination of the program. But a local variable can be accessed in a particular region or the block of code, inside which it is declared and initialized. The scope will be the coding region, where a particular variable can be used and is inaccessible outside that scope.
a = 5 # now this is a global variable that we have declared and it is accessibel till the termination of the program def add(): #this is a simple add function , and invoking this creates a scope , that is inside this function only. a = 4 # These two are the local variables of the add function , and cant be accessed out of scope. b = 3 print(a+b) add() # This will give the result 7 , as computed from the function , because inside the function , we created a local variable and that is used , not the global variable. print(a) # This will print 5 , because here the scope of the local variable in the add function is finished and it cant be used outside that function block , and here we will only use the global declared variable.
Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article. If you want to suggest any improvement/correction in this article please mail us at [email protected]