Movatterモバイル変換


[0]ホーム

URL:


Previous PageUp One LevelNext PagePython Reference ManualContentsIndex
Previous:A.3 Nested scopesUp:A.3 Nested scopesNext:A.3.2 Interaction with dynamic

 
A.3.1 Definitions and rules

Names refer to objects. Names are introduced by name bindingoperations. Each occurrence of a name in the program text refers tothe binding of that name established in the innermost function blockcontaining the use.

Ablock is a piece of Python program text that is executed asa unit. The following are blocks: a module, a function body, and aclass definition.

Ascope defines the visibility of a name within a block. If alocal variable is defined in a block, its scope includes that block.If the definition occurs in a function block, the scope extends to anyblocks contained within the defining one, unless a contained blockintroduces a different binding for the name. The scope of namesdefined in a class block is limited to the class block; it does notextend to the code blocks of methods.

When a name is used in a code block, it is resolved using the nearestenclosing scope. The set of all such scopes visible to a code blockis called the block'senvironment.

If a name is bound in a block, it is a local variable of that block.If a name is bound at the module level, it is a global variable. (Thevariables of the module code block are local and global.) If avariable is used in a code block but not defined there, it is afree variable.

The name binding operations are assignment, class and functiondefinition, import statements, for statements, and except statements.Each assignment or import statement occurs within a block defined by aclass or function definition or at the module level (the top-levelcode block).

If a name binding operation occurs anywhere within a code block, alluses of the name within the block are treated as references to thecurrent block. This can lead to errors when a name is used within ablock before it is bound.

The previous rule is a subtle. Python lacks declarations and allowsname binding operations to occur anywhere within a code block. Thelocal variables of a code block can be determined by scanning theentire text of the block for name binding operations.

If the global statement occurs within a block, all uses of the namespecified in the statement refer to the binding of that name in thetop-level namespace. Names are resolved in the top-level namespace bysearching the global namespace, i.e. the namespace of the modulecontaining the code block, and the builtin namespace, the namespace ofthe module__builtin__. The global namespace is searchedfirst. If the name is not found there, the builtin namespace issearched. The global statement must precede all uses of the name.

The global statement has the same scope as a name binding operationin the same block. If the nearest enclosing scope for a free variablecontains a global statement, the free variable is treated as a global.

A class definition is an executable statement that may use and definenames. These references follow the normal rules for name resolution.The namespace of the class definition becomes the attribute dictionaryof the class. Names defined at the class scope are not visible inmethods.


Previous PageUp One LevelNext PagePython Reference ManualContentsIndex
Previous:A.3 Nested scopesUp:A.3 Nested scopesNext:A.3.2 Interaction with dynamic
Release 2.2.3, documentation updated on 30 May 2003.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2026 Movatter.jp