Namespace in python
In Python, a namespace is a system that assigns a unique name to each and every object. A variable or a method can be considered an object. Python has its own namespace, which is kept in the form of a Python dictionary. Let's look at a directory-file system structure in a computer as an example. It should go without saying that a file with the same name might be found in numerous folders. However, by supplying the absolute path of the file, one can be routed to it if desired.
In the real world, a namespace in python serves the same purpose as a surname. There may be more than one "Alice" in the class, but when you specifically ask for "Alice Lee" or "Alice Clark" (with a surname), there will only be one (for the time being, don't assume that both first and surname are the same for numerous students).
Similarly, based on the namespace, the Python interpreter understands what particular function or variable one is trying to point to in the code. As a result, the word's division provides a little more information. Its Name (a name that serves as a unique identifier) + Space (which talks something related to scope). A name could be any Python function or variable, and space is determined by the position from where the variable or method is being accessed.
Types Of Namespace
When the Python interpreter runs without any user-defined modules, functions, or classes, it is known as a pure Python interpreter. Some functions, such as print() and id(), are always present because they are part of the built-in namespace. A global namespace is formed when a user builds a module, and a local namespace is created afterwards when local functions are added. The global namespace is included in the built-in namespace, while the local namespace is included in the global namespace.
The lifetime of a namespace :
A namespace's lifetime is determined by the scope of objects; if the scope of an object expires, the namespace's lifetime expires as well. As a result, objects in the inner namespace cannot be accessed from the outer namespace.
Example:
Python 3
var1 is in the global namespace
var1 = 5
def some_func():
# var2 is in the local namespace
var2 = 6
def some_inner_func():
# var3 is in the nested local
# namespace
var3 = 7
As illustrated in the diagram, the same object name can appear in different namespaces since their namespace maintains separation between them.
However, in some circumstances, such as the one shown below, you may only be interested in updating or processing global variables, in which case you should explicitly identify the variable as global and update or process it. Note that the line "count = count +1" uses the global variable because it refers to it, but compare that to the same line written "count = 1." Then, according to scope rules, the line "global count" is required.
Python program processing
global variable
count = 5
def some_method():
global count
count = count + 1
print(count)
some_method()
Output
6
The scope of a Python object refers to the coding region from which it can be accessed. As a result, one cannot access any particular object from anywhere in the code; access must be permitted by the object's scope.
Let's look at an example to get a better idea of what I'm talking about:
Example 1
Python program showing
a scope of object
def some_func():
print("Inside some_func")
def some_inner_func():
var = 10
print("Inside inner function, value of var:",var)
some_inner_func()
print("Try printing var from outer function: ",var)
some_func()
Output
Inside some_func Inside inner function, value of var: 10 Traceback (most recent call last): File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 8, in some_func() File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 7, in some_func print("Try printing var from outer function: ",var) NameError: name 'var' is not defined
InsideAIML is the platform where you can learn all the AI based content by the help of certain types of courses.
Comments
Post a Comment