Python 虚拟机是一个栈机器,数据存放在栈中,虚拟机不断执行字节码中的指令,操作数据入栈、出栈。
虚拟机会创建栈帧 (Frame) 作为执行环境(即命名空间),当一个函数调用另外一个函数时,虚拟机会为被调用函数创建一个新的栈帧。
+-------------------------+ +-------------------------+
| | | Block stack |
+-----> | Call frame(bar) +------> +-------------------------+
| | | | Data stack |
| +-----------+-------------+ +-------------------------+
| |
| +-----------v-------------+ +-------------------------+
| | | | Block stack |
Call stack | Call frame(foo) +------> +-------------------------+
| | | | Data stack |
| +-----------+-------------+ +-------------------------+
| |
| +-----------v-------------+ +-------------------------+
| | | | Block stack |
| | Call frame(main) +------> +-------------------------+
+-----> | | | Data stack |
+-------------------------+ +-------------------------+
定义:
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 */
/* As of 2.3 f_lineno is only valid when tracing is active (i.e. when
f_trace is set) -- at other times use PyCode_Addr2Line instead. */
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;LGB 规则:Locals -> Globals -> Builtins
# main.py
a = 1
b = 1
def f():
a = 2
print a # 输出 2
print b # 输出 1
print a # 输出 1
print b # 输出 1书中提到一个另许多初学者疑惑的例子:
a = 1
def f():
print a
a = 2
f()上述代码执行时会报错:UnboundLocalError: local variable 'a' referenced before assignment。
常见的误解是:在执行到函数 f 的 print 语句时,由于 local 作用域没有 a,所以会去外层作用域寻找 a,找到 a=1,因此打印 1。
实际上 Python 在解析(或者说编译)函数 f 的定义时,发现赋值语句a = 2,则会在函数 f 的 local 作用域创建 a,但是并没有对其初始化,直到执行a = 2时才会对 a 进行赋值。在执行print a时,在 local 作用域找到了a,但是由于没有初始化,所以才会出现UnboundLocalError: local variable 'a' referenced before assignment错误。
LEGB 规则: Locals -> Enclosing -> Globals -> Builtins
# main.py
a = 1
def f():
a = 2
def g():
print a
return g
g = f()
g()
# 输出 2