Python namespace detailed

Source: Internet
Author: User
Tags builtin
In layman's terms, the so-called namespace in Python can be understood as a container. Many identifiers can be installed in this container. Identifiers that have the same name in different containers do not conflict with each other. Understanding the Python namespace requires three rules:

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

Second, the function definitions (including def and Lambda) produce a new namespace .

Thirdly, theorder in which Python searches for an identifier is "LEGB".

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

These three rules are more clearly 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 take a closer look at the identifiers in this piece of code.
Line 1th, the first rule, "Assignment generation identifier", produces an identifier G. The assigned location 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, which is int. So where does int define it? Because int is a built-in function that is defined in the __BUILTIN__ module, int is in the layer namespace of ' B '.
Line 2nd, applicable to the first rule, because def contains an implicit assignment process, 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 applies to the second rule, which produces a new namespace.
Line 3rd, apply the first rule, generate an identifier E, and since this is within a function definition, and there is also a function definition inside, so 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 in which it is located is not the same.
Line 5th, which applies the first rule, produces an identifier infunc in the ' E ' layer namespace. As with line 2nd, this line defines a function that also produces a new namespace.
Line 6th, apply the first rule, produce an identifier L, because this l is inside a function, and there is no other function within this function definition, so l is in the ' L ' layer namespace.
Line 7th, applicable to the third rule, the Python interpreter first see the identifier G, in accordance with the order of LEGB to look up, first find the L layer (that is, inside Infunc), No. Find the E-layer, with a value of 10. So the value of G here is 10. Finding the process so far will not find the ' G ' layer. The process of finding e is also the same, with the value of E being 2. So the result of line 9th is 12.

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

As can be seen from the above example, if the same identifier is defined in a different namespace, there is no correlation and no conflict occurs. The process of finding the value of an identifier is always looked up from the current layer, and the value of the identifier is first found. It is also possible to say that the ' B ' layer identifier is available in all modules (. py files); The G ' layer identifier is available within 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 off ():    g = ' Out '    def Inter ():     Global g          prin T s,g  Inter () >>> out () ===> ' in global '

As you can see, although there are two layers of G, but after using the global statement, it refers to the ' G ' layer identifier. That is, the G in line 7th, that is, the 1th line produces the G, the value is ' global '.

Finally, in fact, as long as the programming time to note that do not use the same identifier, you can basically avoid any namespace-related problems. There is also in a function to try not to use the identifier in the upper namespace, if it must be used, it is best to use the method of parameter passing, which is conducive to maintaining the independence of the function.

  • Related Article

    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.