Python namespace details, python namespace

Source: Internet
Author: User
Tags builtin

Python namespace details, python namespace

In general, the so-called namespace in Python can be understood as a container. Many identifiers can be installed in this container. Identifiers with the same name in different containers do not conflict with each other. To understand the python namespace, you must have three rules:

First, assign values (including explicit and implicit values) to generate identifiers. The location of the assignment determines the namespace in which the identifiers reside.

Second, function definitions (including def and lambda) generate new namespaces.

Third, the sequence for searching an identifier in python is "LEGB ".

The so-called "LEGB" is the abbreviation of the first letter of the English name of the four-layer namespace in python.
The innermost layer is L (local), which indicates that it is in a function definition and does not include the function definition in this function.
The second layer E (enclosing function) is represented in a function definition, but this function also contains the function definition. In fact, the L layer and the E layer are only opposite.
Level 3G (global) refers to the namespace of a module, that is, the identifier defined in A. py file, but not in a function.
Layer-4 B (builtin) refers to the namespace that the python interpreter already has at startup, builtin is called because the python interpreter automatically loads the _ builtin _ module when it is started. The list, str, and other built-in functions in this module are in the namespace of layer B.

The three rules can be found through an example. As shown in the following example:

>>> g = int('0x3', 0)>>> def outFunc():  e = 2    g = 10  def inFunc():     l = 1     return g + e    return inFunc()>>> outFunc() ===> 12

For more information, see the identifiers in this Code.
Row 3 applies the first rule "assign value to generate identifier", so an identifier g is generated. "The location of the assignment determines the namespace of the identifier", because g is not in a function definition, so g is in the namespace of the 'G' layer. There is an identifier in this row, that is, int. Where is int defined? Because int Is a built-in function defined in the _ builtin _ module, int Is In The namespace of the 'B' layer.
Row 3 applies the first rule. Because def contains an implicit value assignment process, this row generates an identifier outFunc, which is not within the definition of a function. Therefore, outFunc Is In The namespace of the 'G' layer. In addition, this row applies the second rule to generate a new namespace.
Row 3 applies the first rule and generates an identifier e. Since this is within a function definition and there is a function definition internally, e is in the namespace of the 'E' layer.
Line 1 should note that the first rule is applied to generate an identifier g, which is the same as e and is not in the namespace of the 'E' layer. This g is different from the g in the first line because it is in a different namespace.
Row 3 applies the first rule to generate an identifier inFunc in the namespace of the 'E' layer. Like row 2nd, this row of definition functions also generate a new namespace.
Row 3 applies the first rule and generates an identifier l. Because this l is inside a function and there is no definition of other functions in this function, therefore, l is in the namespace of the 'l' layer.
Row 3 applies to the third rule. The python interpreter first looks at the identifier g and goes up in the LEGB order. First, it looks for the L layer (that is, inside inFunc). No. Find the E layer again. Yes, the value is 10. Therefore, the value of g here is 10. The search process ends and the 'G' layer is not found. The process of searching for e is the same. The value of e is 2. Therefore, the result of the 9th rows is 12.

In fact, the so-called "LEGB" is created for academic convenience. It makes no sense for a programmer to tell which identifier is at which layer. Just know how python looks for its value for an identifier. In fact, the process of searching for values is intuitive and easy to understand.

The above example shows that it is irrelevant to define the same identifier in different namespaces and there is no conflict. The process of searching for a value of an identifier always starts from the current layer. The value first is the value of this identifier. It can also be said that the 'B' layer identifier is in all modules (. the 'G' layer identifier is in the current module (. 'E' and 'l' layer identifiers are available in the current function.

Let's look at an example to explain the usage of global statements. The Code is as follows:

>>> g = 'global'>>> s = 'in'>>> def out():    g = 'out'    def inter():     global g          print s,g  inter()>>> out() ===> 'in global'

We can see that although there are two layers of g, after using the global statement, it refers to the identifier of the 'G' layer. That is, the g in the row 7th refers to the g generated by the row 1st and the value is 'global '.

In the last sentence, you only need to pay attention when programming. Instead of using the same identifier, you can basically avoid any problems related to namespaces. In addition, try not to use the identifiers in the upper namespace in a function. If you must use the identifiers in the upper namespace, it is best to use the parameter transfer method, which is conducive to maintaining the independence of the function.


How does Python parse large XML documents with namespaces?

What effect do you want to achieve by parsing this file?
 
Python namespace Problems

Traceback (most recent call last ):
File "<pyshell #7>", line 1, in <module>
F ()
File "<pyshell #6>", line 2, in f
A = a + 1
UnboundLocalError: local variable 'A' referenced before assignment
First, in the f () function, a is a local variable. Cause: the phrase global a is not added.
Then, the function content calls the value of a as soon as it comes up. For python, it does not know what a is.
Therefore, this error means that, as a local variable, a is referenced without declaring a value first, and its value cannot be searched.

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.