從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
A 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
相關推薦
從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, unl
python類中儲存非繫結方法作為成員函式
習慣了函式式,動不動傳一個函式。但是直接把函式作為類方法儲存,再呼叫時會報錯。 舉一個unittest時的例子 class MyTestCase(unittest.TestCase): @classmethod def setUpClass(cls): pr
jquery中取消和繫結hover事件的正確方式
在網頁設計中,我們經常使用jquery去響應滑鼠的hover事件,和mouseover和mouseout事件有相同的效果,但是這其中其中如何使用bind去繫結hover方法呢?如何用unbind取消繫結的事件呢? 一、如何繫結hover事件 先看以下程式碼,假設我們
C++中的靜態繫結和動態繫結
(感謝原作者分享:http://www.cnblogs.com/lizhenghn/p/3657717.html) C++在面向物件程式設計中,存在著靜態繫結和動態繫結的定義,本節即是主要講述這兩點區分。 我是在一個類的繼承體系中分析的,因此下面所說的物件一般就是指一個類的例項。 首先我們需要明確幾個名詞定
(C++)C++多型性中的靜態繫結和動態繫結
靜態繫結和動態繫結是C++多型性的一種特性。 1、物件的靜態型別和動態型別: 物件的靜態型別: 物件在宣告是採用的型別,在編譯期確定; 物件的動態型別: 當前物件所指的型別,在執行期決定,物件的動態型別可以更改,但靜態型別無法更改。 class B{ }; clas
C# WinForm中ListView 的繫結和讀取方法
根據自己的應用方式和在網上搜尋的資料,整理一下ListView控制元件的應用。 ListView控制元件可以直觀的顯示資料,操作很方便簡單的特點。 一、ListView新增表頭的兩種方法: 1、直接在控制元件的任務中的編輯列中新增,Name是繫結的code值,Text是表頭的顯示值。 2
如何在Adobe DW CC中新增伺服器行為、資料庫和繫結面板
Adobe DW CC 2015對HTML5、CSS3、jQuery、jQuery UI都有很好的支援,無奈這個版本卻未提供開發動態網站所需要的伺服器行為面板、資料庫面板以及繫結面板等。要新增這個面板,可通過以下步驟來實現。 安裝完成後在軟體中登陸賬號,搜
從python容器中隨機選取元素
1 # 1.使用python random模組的choice方法隨機選擇某個元素 2 import random 3 4 foo = ['a', 'b', 'c', 'd', 'e'] 5 from random import choice 6 7 print(choice(foo))
vue中父子元件繫結事件
父子元件繫結事件: 注意:父元件是自定義事件,由子元件傳遞觸發事件資訊;子元件是系統事件比如:click等 <div id="app"> <counter @handle="fatherEvent"></counter>
td中通過button繫結click事件實現跳轉,以及獲取同一table中另一td的值
1、通過button繫結事件跳轉到同一頁面的其他div 例: js頁面 <table id = "tableid"> <tr> <td id = "rcode">"123456"</td> <td><butto
vue2.0中v-on繫結自定義事件
vue中父元件通過prop傳遞資料給子元件,而想要將子元件的資料傳遞給父元件,則可以通過自定義事件的繫結。 每個 Vue 例項都實現了事件介面,即: 使用 $on(eventName) 監聽事件 使用 $emit(eventName) 觸發事
Vue學習(二)——內容顯示和繫結事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Vue入門</title> <script src="./vue.js"
【轉】python類中super()和__init__()的區別
【轉】python類中super()和__init__()的區別 單繼承時super()和__init__()實現的功能是類似的 class Base(object): def __init__(self): print 'Base create' class child
Java中的靜態繫結與動態繫結事例
在這個例子中使用Java面向物件中的多型來簡單介紹靜態繫結和動態繫結的區別。 /* * 建立一個Father類。 * 為了方便獲取,成員變數a設定為public的。 * */ class Father{ public int a = 10; public void saying(
python學習(五):Python類中super()和__init__()的關係
1.單繼承時super()和__init__()實現的功能是類似的 class Base(object): def __init__(self): print 'Base create' class childA(Base): def __init__(sel
angular4中ngModel雙向繫結在限制輸入情況下與value值不一致的問題及嘗試的解決方法
近期公司的一個後臺專案在使用angular4進行開發,隨著專案的推進和迭代開發,需要對有的input框的value值進行限制輸入,例如目前專案的一些input框只允許輸入數字和'.' 這兩種形式的內容。就在input標籤的中添加了(keyup)屬性,將相應的ts檔案中編寫的一個正則驗證方法賦值給該屬
js中this的繫結規則及優先順序
一. this繫結規則 函式呼叫位置決定了this的繫結物件,必須找到正確的呼叫位置判斷需要應用下面四條規則中的哪一條。 1.1 預設繫結 看下面程式碼: function foo() { console.log(this.a); } var a =
利用JS實現vue中的雙向繫結
Vue 已經是主流框架了 它的好處也不用多說,都已經是大家公認的了 那我們就來理解一下Vue的單向資料繫結和雙向資料繫結 然後再使用JS來實現Vue的雙向資料繫結 單向資料繫結 指的是我們先把模板寫好,然後把模板和資料(資料可能來自後臺)整合到一起形成HTML程式碼,然後把這段HTML程式碼插入到文件流裡面
ES6箭頭函式中的this繫結問題
關於this指向問題的討論一直是學習js不可忽視的重要部分,那些一個又一個圍繞this挖的筆試坑,彷彿永遠也填不完 var obj={ fn:function(){ console.log(this); } } obj.fn();//object
理解Javascript中的事件繫結與事件委託(轉載)
最近在深入實踐js中,遇到了一些問題,比如我需要為動態建立的DOM元素繫結事件,那麼普通的事件繫結就不行了,於是通過上網查資料瞭解到事件委託,因此想總結一下js中的事件繫結與事件委託。 事件繫結 最直接的事件繫結:HTML事件處理程式 如下示例程式碼,通過節點屬性顯式宣