gpt4 book ai didi

python - 在循环中创建实例变量返回 None 对象

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:18 26 4
gpt4 key购买 nike

我尝试了一个小的 trafficlight 实现并得到了以下代码:

def __init__(...):
...
self.redLight = Light(color = "red", master = self.frame)
self.redLight.place(x = 10, y = 10)
self.yellowLight = Light(color = "yellow", master = self.frame)
self.yellowLight.place(x = 10, y = 40)
self.greenLight = Light(color = "green", master = self.frame)
self.greenLight.place(x = 10, y = 70)
...

我感到无聊并尝试在循环中定义这个相当冗余的代码:

def __init__(...):
...
self.redLight = None
self.yellowLight = None
self.greenLight = None

for l in [[self.redLight, "red", 10],
[self.yellowLight, "yellow", 40],
[self.greenLight, "green", 70]]:
l[0] = Light(color = l[1], master = self.frame)
l[0].place(x = 10, y = l[2])
...

我的理解是,它与第一个代码示例完全相同,但事实证明,它没有写入实例变量。当我在调试器中查看代码时,l[0] 对象是一个 Light-object...

python不是引用调用,l[0]应该直接写实例变量吗?

最佳答案

这就是 setattr getattr 用于:

def __init__(self):
for l in [['redLight', "red", 10],
['yellowLight', "yellow", 40],
['greenLight', "green", 70]]:
setattr(self, l[0], Light(color=l[1], master=self.frame))
getattr(self, l[0]).place(x=10, y=l[2])

因为如分配给 l[0] 的评论中所述只是改变局部变量的内容 l不会设置您的实例的属性。

请注意,这归结为:

>>> a = 1
>>> b = a
>>> b = 2 # assigning a new value to "b" doesn't change "a"!
>>> a
1

或:

>>> a = 1
>>> b = [a]
>>> b[0] = 2 # won't change "a".
>>> a
1

因此,虽然您可以通过分配给 self.redLight 来更改属性并不意味着您可以通过将不同的值分配给包含对 self.redLight 值的引用的列表来更改属性。 .

关于python - 在循环中创建实例变量返回 None 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46146278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com