Python 3.2 Official Document translations: scopes and namespaces

Source: Internet
Author: User
Tags definition bind class definition execution variables

Before I introduce the class, I want to tell you some rules about the Python scope first. The definition of a class uses namespaces very cleverly, and you need to know how the scope and namespace work to fully understand what happens next. Incidentally, the knowledge about this section is useful for any good Python programmer.

Let's start with some definitions.

A namespace (namespace) is a mapping from name to object. Most namespaces are currently implemented in a Python dictionary, but that is usually not noticed (except for performance), which can be changed in the future. Examples of namespaces are: set with built-in names (including functions such as ABS () and built-in exception names), global variable names in the module, and local names when the function is called. To a certain extent, the property assignment of an object forms a namespace. The important thing in mastering namespaces is that there is absolutely no relationship in different namespaces. For example, two different modules can be maximize without confusing the definition method. The user of the module must be prefixed with the module name.

I'm used to it. Every property that follows the dot (.) is called an attribute. For example, in an expression z.real. Real is an attribute of Object Z. In a strictly speaking sense, the name referenced in the module is a reference to the attribute: the expression Modname.funcname,modname is a Module object and Funcnam is one of its properties. In this example, this happens to be a simple mapping between the module properties and the global variable names in the module definition: They share the same namespace.

The property is either readable or writable. In the latter case, the property is allowed to be assigned a value. If the module properties are writable, you can write this, Modername.the_answer = 42. Writable properties can also be deleted with the DEL statement. For example, Del Modname.the_answer will remove the attribute The_answer from the name ModName module.

Namespaces can exist at different times and have different lifecycles. When the Python interpreter starts, the namespace that contains the built-in name is created. And never deleted. The module's global namespace is created when the module definition is read-ins. Normally, the module namespace persists until the interpreter exits. Through the interpreter's top-level call execution, reading or interacting from the script file is considered part of the _main_ module, so they also have their own global namespaces. (built-in names actually exist in a module, called Builtins.)

When a function is called, the local namespace of the function is created, and when the function returns a value or throws an exception that is not handled in the method, it is deleted. Of course, each recursive call has its own local namespace.

A scope is a Python program that can directly access a namespace's body area. "Direct access" here means that an illegal reference to a name attempts to find a name in the namespace.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Although scopes are statically defined, they are used dynamically. At any time during execution, there are at least three scopes to which the associated namespaces can be accessed directly:

l was first searched for the most inner scopes containing local variables.

L Any scope that closes the function, which is queried with the most recently encapsulated scope, and contains not local variables or non-global variables.

L then query the scope that contains the current module global variable.

L The last query is the outermost scope, which is the namespace that contains the built-in methods.

If the name is defined as global, all references and assignments can be directed to the intermediate scope that contains the module global variable. The nonlocal statement can be used in order to rebind the variables found in the most inner-layer extraterritorial surface. If not defined as non-local, this variable can only be read. (an attempt to read such a variable produces a local local variable in the outermost scope, while the external variable of the same identifier does not change)

Typically, local scopes refer to local variables of the current function. Outside the function, the local scope refers to the same namespace as the Global action: the module namespace. The class definition also introduces another namespace in the local scope.

It is important to know that scopes can be defined in text. The global scope of a function in a module is defined as the namespace of that module, regardless of where the function is invoked or what name it is called. On the other hand, a true query for names is dynamically queried at run time. However, the definition of the language is evolving toward the compile-time static name, so do not rely on dynamic name resolution. (In fact, local variables have been statically defined.)

One of the special thing about Python is that if no global variables are valid-the assignment of names often goes into the inner-most range. Assignments do not copy data-they bind names tightly to objects. The same is true for deletion. The DEL statement removes the binding to X from the namespace of the local scope. In fact, all operations that introduce a new name use local variables, in particular, import statements and function definitions to bind a module or function name in a local local scope.

The global statement can be used to describe the special variables that are active in the global scope and should be bound there. The nonlocal statement describes the special variables that are active in the encapsulated scope and binds there.

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.