python yield學習

來源:互聯網
上載者:User

標籤:方法   variant   field   保留   多個   ini   nec   lob   list   

yield的功能類似於return,但是不同之處在於它返回的是產生器。

產生器
產生器是通過一個或多個yield運算式構成的函數,每一個產生器都是一個迭代器(但是迭代器不一定是產生器)。

如果一個函數包含yield關鍵字,這個函數就會變為一個產生器。

產生器並不會一次返回所有結果,而是每次遇到yield關鍵字後返回相應結果,並保留函數當前的運行狀態,等待下一次的調用。

由於產生器也是一個迭代器,那麼它就應該支援next方法來擷取下一個值。

基本操作

# 通過`yield`來建立產生器
def func():
for i in xrange(10);
yield i

# 通過列表來建立產生器
[i for i in xrange(10)]
# 調用如下
>>> f = func()
>>> f # 此時產生器還沒有運行
<generator object func at 0x7fe01a853820>
>>> f.next() # 當i=0時,遇到yield關鍵字,直接返回
0
>>> f.next() # 繼續上一次執行的位置,進入下一層迴圈
1
...
>>> f.next()
9
>>> f.next() # 當執行完最後一次迴圈後,結束yield語句,產生StopIteration異常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>>
除了next函數,產生器還支援send函數。該函數可以向產生器傳遞參數。

>>> def func():
... n = 0
... while 1:
... n = yield n #可以通過send函數向n賦值
...
>>> f = func()
>>> f.next() # 預設情況下n為0
0
>>> f.send(1) #n賦值1
1
>>> f.send(2)
2
>>>
應用

最經典的例子,產生無限序列。

常規的解決方案是,產生一個滿足要求的很大的列表,這個列表需要儲存在記憶體中,很明顯記憶體限制了這個問題。

def get_primes(start):
for element in magical_infinite_range(start):
if is_prime(element):
return element
如果使用產生器就不需要返回整個列表,每次都只是返回一個資料,避免了記憶體的限制問題。

def get_primes(number):
while True:
if is_prime(number):
yield number
number += 1
產生器源碼分析
產生器的源碼在Objects/genobject.c。

調用棧

在解釋產生器之前,需要講解一下Python虛擬機器的調用原理。

Python虛擬機器有一個棧幀的調用棧,其中棧幀的是PyFrameObject,位於Include/frameobject.h。

typedef struct _frame {
PyObject_VAR_HEAD
struct _frame *f_back; /* previous frame, or NULL */
PyCodeObject *f_code; /* code segment */
PyObject *f_builtins; /* builtin symbol table (PyDictObject) */
PyObject *f_globals; /* global symbol table (PyDictObject) */
PyObject *f_locals; /* local symbol table (any mapping) */
PyObject **f_valuestack; /* points after the last local */
/* Next free slot in f_valuestack. Frame creation sets to f_valuestack.
Frame evaluation usually NULLs it, but a frame that yields sets it
to the current stack top. */
PyObject **f_stacktop;
PyObject *f_trace; /* Trace function */

/* If an exception is raised in this frame, the next three are used to
* record the exception info (if any) originally in the thread state. See
* comments before set_exc_info() -- it‘s not obvious.
* Invariant: if _type is NULL, then so are _value and _traceback.
* Desired invariant: all three are NULL, or all three are non-NULL. That
* one isn‘t currently true, but "should be".
*/
PyObject *f_exc_type, *f_exc_value, *f_exc_traceback;

PyThreadState *f_tstate;
int f_lasti; /* Last instruction if called */
/* Call PyFrame_GetLineNumber() instead of reading this field
directly. As of 2.3 f_lineno is only valid when tracing is
active (i.e. when f_trace is set). At other times we use
PyCode_Addr2Line to calculate the line from the current
bytecode index. */
int f_lineno; /* Current line number */
int f_iblock; /* index in f_blockstack */
PyTryBlock f_blockstack[CO_MAXBLOCKS]; /* for try and loop blocks */
PyObject *f_localsplus[1]; /* locals+stack, dynamically sized */
} PyFrameObject;
棧幀儲存了給出代碼的的資訊和上下文,其中包含最後執行的指令,全域和局部命名空間,異常狀態等資訊。f_valueblock儲存了資料,b_blockstack儲存了異常和迴圈控制方法。

舉一個例子來說明,

def foo():
x = 1
def bar(y):
z = y + 2 # <--- (3) ... and the interpreter is here.
return z
return bar(x) # <--- (2) ... which is returning a call to bar ...
foo() # <--- (1) We‘re in the middle of a call to foo ...
那麼,相應的調用棧如下,一個py檔案,一個類,一個函數都是一個代碼塊,對應者一個Frame,儲存著上下文環境以及位元組碼指令。

c ---------------------------
a | bar Frame | -> block stack: []
l | (newest) | -> data stack: [1, 2]
l ---------------------------
| foo Frame | -> block stack: []
s | | -> data stack: [<Function foo.<locals>.bar at 0x10d389680>, 1]
t ---------------------------
a | main (module) Frame | -> block stack: []
c | (oldest) | -> data stack: [<Function foo at 0x10d3540e0>]
k ---------------------------
每一個棧幀都擁有自己的資料棧和block棧,獨立的資料棧和block棧使得解譯器可以中斷和恢複棧幀(產生器正式利用這點)。

Python代碼首先被編譯為位元組碼,再由Python虛擬機器來執行。一般來說,一條Python語句對應著多條位元組碼(由於每條位元組碼對應著一條C語句,而不是一個機器指令,所以不能按照位元組碼的數量來判斷代碼效能)。

調用dis模組可以分析位元組碼,

from dis import dis

dis(foo)

5 0 LOAD_CONST 1 (1) # 載入常量1
3 STORE_FAST 0 (x) # x賦值為1

6 6 LOAD_CONST 2 (<code object bar at 0x7f3cdee3a030, file "t1.py", line 6>) # 載入常量2
9 MAKE_FUNCTION 0 # 建立函數
12 STORE_FAST 1 (bar)

9 15 LOAD_FAST 1 (bar)
18 LOAD_FAST 0 (x)
21 CALL_FUNCTION 1 # 調用函數
24 RETURN_VALUE
其中,

第一行為程式碼號;
第二行為位移地址;
第三行為位元組碼指令;
第四行為指令參數;
第五行為參數解釋。
產生器源碼分析

由了上面對於調用棧的理解,就可以很容易的明白產生器的具體實現。

產生器的源碼位於object/genobject.c。

產生器的建立

PyObject *
PyGen_New(PyFrameObject *f)
{
PyGenObject *gen = PyObject_GC_New(PyGenObject, &PyGen_Type); # 建立產生器對象
if (gen == NULL) {
Py_DECREF(f);
return NULL;
}
gen->gi_frame = f; # 賦予代碼塊
Py_INCREF(f->f_code); # 引用計數+1
gen->gi_code = (PyObject *)(f->f_code);
gen->gi_running = 0; # 0表示為執行,也就是產生器的初始狀態
gen->gi_weakreflist = NULL;
_PyObject_GC_TRACK(gen); # GC跟蹤
return (PyObject *)gen;
}
send與next

next與send函數,如下

static PyObject *
gen_iternext(PyGenObject *gen)
{
return gen_send_ex(gen, NULL, 0);
}


static PyObject *
gen_send(PyGenObject *gen, PyObject *arg)
{
return gen_send_ex(gen, arg, 0);
}
從上面的代碼中可以看到,send和next都是調用的同一函數gen_send_ex,區別在於是否帶有參數。

static PyObject *
gen_send_ex(PyGenObject *gen, PyObject *arg, int exc)
{
PyThreadState *tstate = PyThreadState_GET();
PyFrameObject *f = gen->gi_frame;
PyObject *result;

if (gen->gi_running) { # 判斷產生器是否已經運行
PyErr_SetString(PyExc_ValueError,
"generator already executing");
return NULL;
}
if (f==NULL || f->f_stacktop == NULL) { # 如果代碼塊為空白或調用棧為空白,則拋出StopIteration異常
/* Only set exception if called from send() */
if (arg && !exc)
PyErr_SetNone(PyExc_StopIteration);
return NULL;
}

if (f->f_lasti == -1) { # f_lasti=1 代表首次執行
if (arg && arg != Py_None) { # 首次執行不允許帶有參數
PyErr_SetString(PyExc_TypeError,
"can‘t send non-None value to a "
"just-started generator");
return NULL;
}
} else {
/* Push arg onto the frame‘s value stack */
result = arg ? arg : Py_None;
Py_INCREF(result); # 該參數引用計數+1
*(f->f_stacktop++) = result; # 參數壓棧
}

/* Generators always return to their most recent caller, not
* necessarily their creator. */
f->f_tstate = tstate;
Py_XINCREF(tstate->frame);
assert(f->f_back == NULL);
f->f_back = tstate->frame;

gen->gi_running = 1; # 修改產生器執行狀態
result = PyEval_EvalFrameEx(f, exc); # 執行位元組碼
gen->gi_running = 0; # 恢複為未執行狀態

/* Don‘t keep the reference to f_back any longer than necessary. It
* may keep a chain of frames alive or it could create a reference
* cycle. */
assert(f->f_back == tstate->frame);
Py_CLEAR(f->f_back);
/* Clear the borrowed reference to the thread state */
f->f_tstate = NULL;

/* If the generator just returned (as opposed to yielding), signal
* that the generator is exhausted. */
if (result == Py_None && f->f_stacktop == NULL) {
Py_DECREF(result);
result = NULL;
/* Set exception if not called by gen_iternext() */
if (arg)
PyErr_SetNone(PyExc_StopIteration);
}

if (!result || f->f_stacktop == NULL) {
/* generator can‘t be rerun, so release the frame */
Py_DECREF(f);
gen->gi_frame = NULL;
}

return result;
}
位元組碼的執行

PyEval_EvalFrameEx函數的功能為執行位元組碼並返回結果。

# 主要流程如下,
for (;;) {
switch(opcode) { # opcode為作業碼,對應著各種操作
case NOP:
goto fast_next_opcode;
...
...
case YIELD_VALUE: # 如果作業碼是yield
retval = POP();
f->f_stacktop = stack_pointer;
why = WHY_YIELD;
goto fast_yield; # 利用goto跳出迴圈
}
}

fast_yield:
...
return vetval; # 返回結果
舉一個例子,f_back上一個Frame,f_lasti上一次執行的指令的位移量,

import sys
from dis import dis


def func():
f = sys._getframe(0)
print f.f_lasti
print f.f_back
yield 1

print f.f_lasti
print f.f_back
yield 2


a = func()
dis(func)
a.next()
a.next()
結果如下,其中第三行的英文為作業碼,對應著上面的opcode,每次switch都是在不同的opcode之間進行選擇。

6 0 LOAD_GLOBAL 0 (sys)
3 LOAD_ATTR 1 (_getframe)
6 LOAD_CONST 1 (0)
9 CALL_FUNCTION 1
12 STORE_FAST 0 (f)

7 15 LOAD_FAST 0 (f)
18 LOAD_ATTR 2 (f_lasti)
21 PRINT_ITEM
22 PRINT_NEWLINE

8 23 LOAD_FAST 0 (f)
26 LOAD_ATTR 3 (f_back)
29 PRINT_ITEM
30 PRINT_NEWLINE

9 31 LOAD_CONST 2 (1)
34 YIELD_VALUE # 此時作業碼為YIELD_VALUE,直接跳轉上述goto語句,此時f_lasti為當前指令,f_back為當前frame
35 POP_TOP

11 36 LOAD_FAST 0 (f)
39 LOAD_ATTR 2 (f_lasti)
42 PRINT_ITEM
43 PRINT_NEWLINE

12 44 LOAD_FAST 0 (f)
47 LOAD_ATTR 3 (f_back)
50 PRINT_ITEM
51 PRINT_NEWLINE

13 52 LOAD_CONST 3 (2)
55 YIELD_VALUE
56 POP_TOP
57 LOAD_CONST 0 (None)
60 RETURN_VALUE
18
<frame object at 0x7fa75fcebc20> #和下面的frame相同,屬於同一個frame,也就是說在同一個函數(命名空間)內,frame是同一個。
39
<frame object at 0x7fa75fcebc20>

python yield學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.