Python namespaces detailed _python

Source: Internet
Author: User

In layman's terms, the so-called namespaces in Python can be understood as a container. There are many identifiers that can be loaded in this container. Identifiers with the same name in different containers do not conflict with each other. Understanding the Python namespace requires three rules:

First, assignment (including explicit assignment and implicit assignment) produces an identifier, where the assigned value determines the namespace in which the identifier is located .

Second, the function definition (including def and Lambda) produces a new namespace .

Third,python searches for an identifier in the order of "LEGB".

The so-called "LEGB" is the acronym for the initials of the English initials of the four-tier namespaces in Python.
The innermost layer is L (local), expressed in a function definition, and there is no further definition of the function in this function.
The second layer E (enclosing function) is represented in a function definition, but it also contains the definition of a function, in fact the L and E layers are only relative.
The third layer, G (global), refers to the namespace of a module, that is, the identifier defined in a. py file, but not in a function.
Layer Fourth B (builtin) is the namespace that the Python interpreter already had when it was started, and is called Builtin because the __builtin__ module is automatically loaded when the Python interpreter starts, the list in this module, The built-in functions, such as STR, are in the B-level namespace.

These three rules are understood by an example. As shown in the following example:

>>> g = Int (' 0x3 ', 0)
>>> def outfunc ():
  e = 2
    g = Ten
  def infunc ():
     L = 1
     return G + E return
    infunc ()
>>> outfunc () ===> 12

To see the identifiers in this code in more detail.
Line 1th, which applies the first rule "assignment generation identifier", produces an identifier G. The location of the assignment determines the namespace in which the identifier is located, because G is not in a function definition, so g is in the ' G ' layer namespace. There is also an identifier in this line, that is int. So where is int defined? Since int is a built-in function that is defined in the __BUILTIN__ module, the int is in the layer namespace of ' B '.
Line 2nd, which applies the first rule, because def contains an implicit assignment procedure, this line produces an identifier Outfunc,outfunc is not inside a function definition, so outfunc is in the ' G ' layer namespace. In addition, this line also applies the second rule, which produces a new namespace.
Line 3rd, which applies the first rule, produces an identifier E, and because it is within a function definition and there is a function definition inside, E is in the ' E ' layer namespace.
Line 4th Note that the first rule is applied to produce an identifier G, which is the same as e outside of the ' E ' layer namespace. This g is different from the first line of G, because the namespace is not the same.
Line 5th, which applies the first rule, produces an identifier infunc that is in the ' E ' layer namespace. As with line 2nd, the line definition function also produces a new namespace.
Line 6th, which applies the first rule, produces an identifier L, which is in the ' L ' layer namespace because it is inside a function and there is no definition of other functions within the function.
Line 7th, for the third rule, the Python interpreter first sees the identifier G, looking up in the order of LEGB, first looking for the L layer (i.e. inside the Infunc), not. Find the E-layer again, with a value of 10. So the value of G here is 10. Look for the process so far, and no longer find the ' G ' layer. The process of finding e is the same, and E has a value of 2. So the result for line 9th is 12.

In fact, the so-called "LEGB" is for academic ease of expression and created. It makes sense for a programmer to say which identifier is in which layer, as long as you know how Python looks for its value for an identifier. In fact, the process of finding value is intuitive and easy to understand.

You can also see from the example above that it does not matter if you define the same identifier in a different namespace, and there is no conflict. The process of looking for a value of an identifier is always found from the current layer, and the value of that identifier is the first thing to find. It can thus be said that the ' B ' layer identifier is available in all modules (. py files); The G ' layer identifier is available in the current module (. py file); The E ' and ' L ' layer identifiers are available within the current function.

Let's look at an example to explain the use of the global statement. The code looks like this:

>>> g = ' global '
>>> s = ' in '
>>> def out ():
    g = ' Out '
    def Inter ():
     Global G     
     Print S,g
  Inter ()
>>> out () ===> ' in global '

As you can see, there are two layers of G, but when you use the global statement, you refer to the ' G ' layer's identifier. That is, G in line 7th, which is the G that is generated in line 1th, and the value is ' global '.

Finally, as long as the programming time to pay attention to, do not use the same identifier, basically you can avoid any namespace-related problems. There is also in a function as far as possible not to use the identifier in the upper namespace, if you have to use, it is best to use the method of parameter passing, which is conducive to maintaining the independence of the function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.