Python class 和全局变量与局部变量
Python class and global vs local variables
我无法理解以下代码片段的结果:
my_str = "outside func"
def func():
my_str = "inside func"
class C():
print(my_str)
print((lambda:my_str)())
my_str = "inside C"
print(my_str)
输出为:
outside func
inside func
inside C
另外一段代码是:
my_str = "not in class"
class C:
my_str = "in the class"
print([my_str for i in (1,2)])
print(list(my_str for i in (1,2)))
输出为:
[‘in the class’, 'in the class’]
['not in class’, 'not in class’]
问题是:
- 每个 print() 语句中发生了什么?
- 谁能解释为什么 print() 语句从不同的命名空间获取字符串?
编辑 1:
我认为这与 不同,因为我谦虚地认为那里的答案没有解释这种变化:
my_str = "outside func"
def func():
my_str = "inside func"
class C():
print(my_str)
print((lambda:my_str)())
#my_str = "inside C"
print(my_str)
输出为:
inside func
inside func
inside func
编辑 2:
的确,这是 的副本,因为正如 Martijn Pieters 所说:
The answer there states: If a name is assigned to within a class body,
almost at the start. You assigned to my_str, making it the same case
as y there. Commenting out that line means you are no longer assigning
to my_str, making it the same case as x.
这里有四个作用域:
- 全局范围
- 函数范围
- classbody
- lambda 作用域
创建 class 语句时,class body 作为函数执行,'function' 的本地名称空间用作 class属性。
但是,class body 不是 作用域,因此其行为与函数不同。在一个函数body中,绑定定义范围;分配给函数中的名称并将其标记为本地。在 class 中,分配给一个名称会使它成为 全局 ,直到您实际进行分配。
因此您的 first print()
调用发现 my_str
是全局的,因为 later 在 class body 你绑定的名字。这是 class 个机构未参与范围的结果。
接下来,您定义一个 lambda 并调用它。 my_str
从未在该 lambda 中赋值,因此必须在 parent 范围内找到它。这里的 class body 不是作用域,下一个作用域是函数作用域。这就是该行打印 inside func
.
的原因
最后,您在 class body 中分配给本地名称 my_str
,并打印该本地名称。
删除分配后,class 中的名称将被视为 non-local;第一个和最后一个 print()
语句现在相等,名称只是根据正常的范围规则查找。
我无法理解以下代码片段的结果:
my_str = "outside func"
def func():
my_str = "inside func"
class C():
print(my_str)
print((lambda:my_str)())
my_str = "inside C"
print(my_str)
输出为:
outside func
inside func
inside C
另外一段代码是:
my_str = "not in class"
class C:
my_str = "in the class"
print([my_str for i in (1,2)])
print(list(my_str for i in (1,2)))
输出为:
[‘in the class’, 'in the class’]
['not in class’, 'not in class’]
问题是:
- 每个 print() 语句中发生了什么?
- 谁能解释为什么 print() 语句从不同的命名空间获取字符串?
编辑 1:
我认为这与
my_str = "outside func"
def func():
my_str = "inside func"
class C():
print(my_str)
print((lambda:my_str)())
#my_str = "inside C"
print(my_str)
输出为:
inside func
inside func
inside func
编辑 2:
的确,这是
The answer there states: If a name is assigned to within a class body, almost at the start. You assigned to my_str, making it the same case as y there. Commenting out that line means you are no longer assigning to my_str, making it the same case as x.
这里有四个作用域:
- 全局范围
- 函数范围
- classbody
- lambda 作用域
创建 class 语句时,class body 作为函数执行,'function' 的本地名称空间用作 class属性。
但是,class body 不是 作用域,因此其行为与函数不同。在一个函数body中,绑定定义范围;分配给函数中的名称并将其标记为本地。在 class 中,分配给一个名称会使它成为 全局 ,直到您实际进行分配。
因此您的 first print()
调用发现 my_str
是全局的,因为 later 在 class body 你绑定的名字。这是 class 个机构未参与范围的结果。
接下来,您定义一个 lambda 并调用它。 my_str
从未在该 lambda 中赋值,因此必须在 parent 范围内找到它。这里的 class body 不是作用域,下一个作用域是函数作用域。这就是该行打印 inside func
.
最后,您在 class body 中分配给本地名称 my_str
,并打印该本地名称。
删除分配后,class 中的名称将被视为 non-local;第一个和最后一个 print()
语句现在相等,名称只是根据正常的范围规则查找。