從python API中命名和綁定認識局部變量,全局變量,自由變量

4.2. Naming and binding

4.2.1. Binding of names

1.局部變量

If a name is bound in a block, it is a local variable of that block,

 unless declared as nonlocal or global.  --- 局部變量轉換爲全局變量

2.全局變量

If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) 

3.自由變量

If a variable is used in a code block but not defined there, it is a free variable.


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

If a name is bound in a block, it is a local variable of that block, unless declared as nonlocal or global. If a name is bound at the module level, it is a global variable. (The variables of the module code block are local and global.) If a variable is used in a code block but not defined there, it is a free variable.

注:名稱在程序文本中每次出現所引用的綁定見名稱解析規則

4.2.2. Resolution of names

scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name.

When a name is used in a code block, it is resolved using the nearest enclosing scope. The set of all such scopes visible to a code block is called the block’s environment.

When a name is not found at all, a NameError exception is raised. If the current scope is a function scope, and the name refers to a local variable that has not yet been bound to a value at the point where the name is used, an UnboundLocalError exception is raised. UnboundLocalError is a subclass of NameError.

If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations.

If the global statement occurs within a block, all uses of the name specified in the statement refer to the binding of that name in the top-level namespace. Names are resolved in the top-level namespace by searching the global namespace, i.e. the namespace of the module containing the code block, and the builtins namespace, the namespace of the module builtins. The global namespace is searched first. If the name is not found there, the builtins namespace is searched. The global statement must precede all uses of the name.

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

The nonlocal statement causes corresponding names to refer to previously bound variables in the nearest enclosing function scope. SyntaxError is raised at compile time if the given name does not exist in any enclosing function scope.

The namespace for a module is automatically created the first time a module is imported. The main module for a script is always called __main__.

Class definition blocks and arguments to exec() and eval() are special in the context of name resolution. A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace. The namespace of the class definition becomes the attribute dictionary of the class. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods – this includes comprehensions and generator expressions since they are implemented using a function scope. This means that the following will fail:

class A:
    a = 42
    b = list(a + i for i in range(10))

----

看完名稱和綁定及名稱解析後的思考....

之前一直是看書或聽老師講解所謂變量,常量等云云...不知所以然...

就python而言

1.常量

2.4. Literals

Literals are notations for constant values of some built-in types.

3. Built-in Constants

A small number of constants live in the built-in namespace.

2.變量: 可以和不同的對象綁定和重新綁定的名稱

3.不可變對象

eg: 1.元組, 2.字符串, 3.bytes, 4.數值型對象(int, bool, float)

4.可變對象

eg: 1.列表, 2.字典, 3.byte array

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章