Python3.2 official document translation-scope and namespace

Source: Internet
Author: User

6.2 Python scope and namespace

Before introducing the class, I 'd like to tell you some rules about the python scope. The class definition uses namespaces very cleverly. You need to know the scope and namespace working principles to fully understand what will happen next. By the way, the Knowledge mentioned in this section is very useful to any excellent python programmers.

Let's start with some definitions.

Namespace is a ing from name to object. Most namespaces are currently implemented using the Python dictionary, but they are usually not noticed (unless for performance) and can be changed in the future. The namespace example is: set with built-in names (including functions such as abs () and built-in exception names), global variable names in the module, and local names during function calling. To some extent, the attribute assignment of an object forms a namespace. The important thing to grasp a namespace is that it is absolutely irrelevant in different namespaces. For example, two different modules can be defined in maximize without confusion. Module users must use the module name as the prefix.

To put it simply, I am used to it that every attribute following the dot (.) is called attribute ). For example, in the expression z. real. Real is an attribute of Object z. Strictly speaking, all referenced names in a module are attribute references: In the expression modname. funcname, modname is a module object and funcnam is its attribute. In this example, this is a simple ing between the module attributes and the global variable names in the module definition: they share the same namespace.

Attributes are readable or writable. In the latter case, you can assign values to attributes. If the module attribute is writable, you can write it like this. modername. the_answer = 42. The writable attribute can also be deleted using the del statement. For example, del modname. the_answer will remove the attribute the_answer from the module named modname.

Namespaces can exist at different times and have different lifecycles. When the python interpreter is started, a namespace containing the built-in name will be created. And never deleted. When the module defines read, the global namespace of the module is created. Normally, the module namespace exists until the interpreter exits. Through the top-level invocation of the interpreter, reading or interacting from the script file is considered as part of the _ main _ module, so they also have their own global namespace. (The built-in name actually exists in a module called builtins .)

When a function is called, the local namespace of the function will be created. When the function return value or an exception that is not handled in the method is thrown, it will be deleted. Of course, each recursive call has its own local namespace.

The scope is the body area of the namespace that a python program can directly access. "Direct access" means that an invalid reference of a name attempts to search for a name in the namespace.

Although the scopes are static definitions, they are dynamically used. At any time during execution, at least three associated namespaces can be directly accessed:

L The first check is the innermost scope of the local variable.

L any scope that disables the function. They are queried starting with the recently encapsulated scope, and contain neither local variables nor non-global variables.

L query the scope that contains the global variables of the current module.

L The Last query is the outermost scope, which is the namespace containing the built-in method.

If the name is global, all references and assignments can be directly assigned an intermediate scope to the global variables of the module. The nonlocal statement can be used to rebind variables found outside the inmost scope. If it is not defined as non-local, the variables can only be read here. (The attempt to read this variable will generate a local variable in the innermost scope, while the variable with the same identifier outside will not change)

Generally, the local scope references the local variables of the current function. Outside the function, the local scope references the same namespace as the global namespace: module namespace. The class definition also introduces another namespace in the local scope.

It is very important to know that the scope can be defined in the text. The global scope of the function defined in the module is the namespace of the module, no matter where the function is called or what name it uses. On the other hand, real queries for names are dynamically queried at runtime. However, language definitions are evolving to static names during compilation, so do not rely on dynamic names. (In fact, local variables have been statically defined)

A special feature of Python is that if no global variable is valid, the value assignment of the name often enters the innermost range. Assignment does not copy data-they closely bind names to objects. The same is true for deletion. The Del statement removes the binding to x from the namespace in the local scope. In fact, all operations that introduce the new name use local variables. In particular, the import Statement and Function Definition bind the module or function name in the local scope.

Global statements can be used to describe the special variables of an activity in the Global scope and should be bound there. A Nonlocal statement describes and binds special variables of an activity in the encapsulation scope.

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.