English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about namespaces, the mapping from names to objects, and the scope of variables.
If you have ever read 'The Zen of Python' (type import this in the Python interpreter), the last line points out that,Namespaces are a great idea-Let's do more things!So what are these mysterious namespaces? First, let's see what a name is.
Names (also known as identifiers) are just the names given to objects. Everything in Python isobjects. Names are a way to access the base object.
For example, when we perform an assignment operation a = 2,2is an object stored in memory, whileaare the names associated with them. We can access the base object throughBuilt-in functions Get the address of certain objects (in RAM) using id(). Let's see how to use it.
# Notice: You may get different id values a = 2 print('id(2) =2)) print('id(a) =
Output Result
id(2) = 9302208 id(a) = 9302208
Here, both refer to the same object2, so they have the same id(). Let's do something interesting.
# Notice: You may get different id values a = 2 print('id(a) = a = a+1 print('id(a) = print('id(3) =3)) b = 2 print('id(b) = print('id(2) =2))
Output Result
id(a) = 9302208 id(a) = 9302240 id(3) = 9302240 id(b) = 9302208 id(2) = 9302208
What happened in the above step sequence? Let's explain it with a diagram:
Initially, an object is created2and associated with the name a, when we execute a = a + 1When, a new object is created3,now a is associated with the object
Please note that id(a) and id(3)has the same value.
In addition, when executing b = 2When, the new name b is associated with the previous object2Associated.
This is valid because Python does not need to create a new duplicate object. The dynamic nature of name binding in this way makes Python powerful. Names can refer to any type of object.
>>> a = 5 >>> a = 'Hello World!' >>> a = [1,2,3]
All of these are valid, andaWe will refer to three different types of objects in different examples.FunctionThey are also objects, so names can also refer to them.
def printHello(): print("Hello") a = printHello a()
Output Result
Hello
The same nameaWe can refer to a function, and we can use the name to call the function.
Now that we understand what a name is, we can continue with the concept of namespace.
In short, a namespace is a collection of names.
In Python, you can imagine a namespace as a mapping of each defined name to the corresponding object.
Different namespaces can coexist at a given time, but they are completely isolated.
When we start the Python interpreter, a namespace containing all built-in names is created, and this namespace exists as long as the interpreter is running.
This is why built-in functions (such as id()) and print() can always be used from any part of the program. EachModuleCreate your own global namespace.
These different namespaces are isolated. Therefore, the same names that may exist in different modules will not conflict.
Modules can have various functions and classes. A local namespace is created when a function is called, where all names are defined. Similar to classes. The following diagram may help clarify this concept.
Even though various unique namespaces are defined, we may not be able to access them from every part of the program. The concept of scope comes into play.
Scope is a part of the program from where the namespace can be accessed directly without any prefix.
There are at least three nested scopes at any given moment.
The scope of the current function with local names
The scope of the module with global names
The outermost scope with built-in names
When referencing within a function, the name is searched in the local namespace, then in the global namespace, and finally in the built-in namespace.
If there is another function inside a function, the new scope is nested within the local scope.
def outer_function(): b = 20 def inner_func(): c = 30 a = 10
Here, the variableain the global namespace. Variablebin the local namespace of outer_function() whilecin the nested local namespace of inner_function().
when we are in inner_function()cin our localbin the non-localain the global. We can definecread and assign a new value, but can only be read frombandainner_function().
If we try to assign as a valueb, a new variablebin the local namespace different from the non-localb. When we assign a value, the same thing happensone.
However, if we willadeclared as global a, all references and assignments will be moved to the globala. Similarly, if we want to rebind a variablebIf a variable is assigned, it must be declared as a non-local variable. The following example will further illustrate this point.
def outer_function(): a = 20 def inner_function(): a = 30 print('a =', a) inner_function() print('a =', a) a = 10 outer_function() print('a =', a)
As you can see, the output of the program is
a = 30 a = 20 a = 10
In this program, three different variables are defined in different namespaces.aand corresponding accesses have been made. In the following program,
def outer_function(): global a a = 20 def inner_function(): global a a = 30 print('a =', a) inner_function() print('a =', a) a = 10 outer_function() print('a =', a)
The output of the program is.
a = 30 a = 30 a = 30
Here, because the keyword global is used, all references and assignments point to the global variable a.