Skip to main content
 首页 » 编程设计

python之Python 作用域规则是否符合词法作用域的定义

2025年05月04日15qlqwjy

<分区>

According to my programming language class , 在使用词法作用域

的语言中

The body of a function is evaluated in the environment where the function is defined, not the environment where the function is called.

例如,SML 遵循以下行为:

val x = 1 
fun myfun () = 
    x 
val x = 10 
val res = myfun()  (* res is 1 since x = 1 when myfun is defined *) 

另一方面,Python 不遵循这种行为:

x = 1 
def myfun(): 
    return x 
x = 10 
myfun()  # 10 since x = 10 when myfun is called 

那为什么is Python described as using lexical scoping ?