Namespace is a container that has all the names (of variables/functions/classes) that you define. You can define same names in different namespaces. A name or a variable exists in a specific area of the code which defines its scope. The information regarding binding between the variables/objects is stored in the namespace. There are three types of namespaces or scopes.
1. Built-in Namespace: These are in-built functions that are available across all files or modules.
2. Global Namespace: The global namespace has all the variables, functions, and classes that are available in a single file.
3. Local Namespace: The local namespace are variables defined within a function.
The scopes are nested, which means that the local namespace is nested within a global namespace which is nested within built-in namespace. Each scope has its namespace.
Built-in namespace - Built-in namespace are available across all the files, and module in Python. All functions that you see below print(), tuple(), type() are all built-in function, and belong to this namespace and are available across all files and modules in Python.
>>> list1 = [1,2,3,4,5,6]
>>> tup1 = tuple(list1)
>>> type(tup1)
<class 'tuple'>
>>> print("Hi")
Hi
>>>
Global namespace - Look at the following code:
x = 20
x += y
print(x)
When we execute this code, it generates a Name Error:
Output:
Traceback (most recent call last):
File "F:\2020 - BPB\input.py", line 2, in <module>
x += y
NameError: name 'y' is not defined
>>>
This is because Python looks for the name y in the global namespace, and fails to find it. It then looks for it in the built in namespace, and does not find it again. Hence, an error is generated. The following code works fine and does not produce any error because the statement y = 5 created a global namespace:
x = 20
y = 5
x += y
print(x)
Output:
25
>>>
Local namespace - Now, let’s look at another example.
x = 20
def print_x():
x = 10
print('Local variable x is equal to ',x)
print('Global variable x is equal to ',x)
print_x()
Output:
Global variable x is equal to 20
Local variable x is equal to 10
>>>
When a call is made to the function, the Python interpreter tries to locate the local variable called x. If that is not available, it will look for x at global namespace.
Code:
x = 20
def print_x():
print('Local variable x is equal to ',x)
print('Global variable x is equal to ',x)
print_x()
Output:
Global variable x is equal to 20
Local variable x is equal to 20
>>>
Local variables, that is, the variables within a function are created when a call is made to that function. Whenever a call is made to a function, a new scope is created, and variables are assigned to that scope. Once the function has been executed, its scope is also gone. In the first example, when the function print_x() was called, it was able to find a local variable x = 10 within the local namespace, and used it up. This value of x existed within the function, and vanishes with the function after its execution is over.
Sometimes, when we want to use the global variable inside our function namespace, we should use global keyword for that variable to make it clear that we want to use the global variable only. Look at the following code:
x = 20
def print_x():
global x
x = 10
print('Local variable x is equal to ',x)
print('Global variable x is equal to ',x)
print_x()
print('Global variable x is equal to ',x)
The moment global keyword is used, the function print_x() comes to know that the global variable x will be used. In the next statement x = 10, the value 10 is assigned to x which is a global variable. Therefore, you will see in the output the value of global variable is 20 before the function is called, and 10 after the function is called. When you are using a global keyword with a variable name within a function, Python will not allow you to create another variable with the same name within the same function.
0 comments:
Post a Comment