Created
December 16, 2013 14:24
-
-
Save kjelly/7987698 to your computer and use it in GitHub Desktop.
python 在處理 closure 時,編譯時期就會偵測是否有使用 closure ,有的話,則會特別產生一些給 closure 用的 opcode。下面例子,會產生一些給 closure 用的 opcode。如果你把 print value 改成其他名稱,則不會產生 closure 的 opcode。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_func(): | |
value = 's' | |
def test(): | |
print value | |
return test | |
import dis | |
dis.dis(get_func) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
get_func 的 value 叫做 co_cellvars
test的 value 叫做 co_freevars