Python variable Scope (i)

Source: Internet
Author: User
Tags variable scope

When using variable names in a program, python creates, alters, or looks for variable names in the so-called Namespaces. Scope refers to the Namespace.

The variable names in Python have been created on the first assignment and must be assigned before they can be used. Since the variable name is not initially declared, Python will have a variable name

The assigned location is associated with a specific namespace. That is, the place in the code where a variable is assigned determines which namespace the variable will exist in, and

Is the extent to which it is visible.

The function adds an extra namespace to the program, in addition to packing the code: in the default case, all the variable names of a function are the namespace of the Function.

Associated with The. This means: 1, a variable name defined within DEF can be used by code within def. You cannot reference such a variable name outside the Function.

2. The variable name in Def does not conflict with a variable name other than def, even if the same variable name is used Elsewhere. A variable that is assigned outside the Def X with the def in this

The variable x in the assignment is a completely different variable.

The function defines the local scope, and the module defines the global Scope. These two scopes have the following relationship:

1. The embedded module is a global scope. Each module is a global scope. An external global variable becomes a property of a module object, but in a module you can

Used just like a simple variable.

2, the scope of the global scope is limited to a single file. The global point here is that the variable name at the top level of a file is global only for the code inside the File.

3. Each call to a function creates a new local scope. That is, the namespace of the variable created by that function will Exist. You can think of every DEF statement

A new local scope is justified, but since Python allows a function to call itself in a loop, technically, the local scope actually corresponds to the invocation of the Function. In other words

says that each function call creates a new local namespace. Recursion is a useful tool when dealing with process structures that cannot be predicted in Advance.

4. The assigned variable name is a local variable unless it is declared as a global variable or a non-local variable.

5. All other variable names can be categorized as local, global, or Built-in.

The principle of variable name resolution Follows: LEGB principle

1. When using an unauthenticated variable name in a function, python searches for 4 scope "local scope (L), followed by the local scope of def or lambda in the previous layer structure (E),

This is followed by the global scope (G), finally the built-in scope (B), and stops at the first place where the variable name can be found. If the variable name is not searched, Python will Error.

2. When assigning a variable name to a function, python always creates or alters the variable name of the local scope, unless it is declared as a global variable in which Function.

3. When assigning values to a variable name outside of a function, the local scope is the same as the global Scope.

Summary of Global Variables:

1. The global variable is the variable name at the top level inside the module

2. If a global variable is assigned within a function, it must be declared

3. Global variables can be used inside a function without being declared

According to the required object, factory functions are sometimes called closures, a function that remembers the value of a variable in a nested scope, even if that scope no longer exists. Factory functions are sometimes used to

Timely generation of event processing, real-time feedback on different situations in the Program. For example:

1 defmaker (N):2     defAction (X):3         returnx**N4     returnAction5 6 7F = Maker (2)8F (3)9Run Result: 9TenF (4) oneRun Result: 16 a  -F = Maker (3) -F (3) theRun Result: 27 -F (4): -Run Result: 64

Python variable Scope (i)

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.