gpt4 book ai didi

python - 类外字典阅读行为

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

我发现了这种我无法理解的行为。我声明这是对我遇到的问题的简化,因为我脚本中的字典有更多的项目。

configuration = {
"video": {
"fullscreen": {
"user": None,
"default": False
}
}
}

class test():

fullscreen = configuration["video"]["fullscreen"]["user"]

def __init__(self):
print(configuration)
print(configuration["video"]["fullscreen"]["user"])
print(self.fullscreen)

if __name__ == "__main__":
configuration["video"]["fullscreen"]["user"] = True
t = test()

这是结果:

{'video': {'fullscreen': {'user': True, 'default': False}}}
True
None

为什么第三次打印的结果是“None”?

最佳答案

实际上,到目前为止对您的问题给出的解释并没有真正向我完全阐明您的合法问题中的指令执行顺序。我想我完全理解你的意思,但我也很困惑

下面的示例将向您展示类属性user_conf [重命名以避免聚焦错误点]是在运行configuration["video"]["fullscreen"][之前创建的main() 中的 "user"] = "John"。换句话说 - 在纯类属性级别 - 它的值是根据 configuration 蓝图设置的。只有类构造函数 - 在 main 之后调用 - 稍后更新该值

configuration = {
"video": {
"fullscreen": {
"user": None,
"default": False
}
}
}
# correcting global variable blueprint
# configuration["video"]["fullscreen"]["user"] = "John"

class test():

print(configuration["video"]["fullscreen"]["user"])
user_conf = configuration["video"]["fullscreen"]["user"]
print(user_conf)

def __init__(self):
# printing modified global variable, all right
print(configuration)
print(configuration["video"]["fullscreen"]["user"])
print(self.user_conf)
self.user_conf = "Jack"
print(self.user_conf)

def main():
# modifying global variable later
# at this point the class attribute user_conf has already been assigned with the old value
configuration["video"]["fullscreen"]["user"] = "John"
test()

if __name__ == '__main__':
main()

请注意,如果您在 main 中注释值更新并取消注释我添加的这些行:

# correcting global variable blueprint
# configuration["video"]["fullscreen"]["user"] = "John"

在配置声明之后,您将得到没有您期望找到的任何 None 的输出,因为类属性将由“更正”的蓝图创建。在这种情况下,您将获得:

John
John
{'video': {'fullscreen': {'user': 'John', 'default':False}}}
John
John
Jack

在第 6 点调整样本的另一种方法 here :

def outer():

configuration = {
"video": {
"fullscreen": {
"user": None,
"default": False
}
}
}
print("initial outer configuration:", configuration)

def inner():
nonlocal configuration
'''
configuration = {
"video": {
"fullscreen": {
"user": "John",
"default": False
}
}
}
'''
configuration["video"]["fullscreen"]["user"] = "John"
print("inner configuration:", configuration)

inner()
print("modified outer configuration:", configuration)

outer()

这会给出:

initial outer configuration: {'video': {'fullscreen': {'user': None,'default': False}}}
inner configuration: {'video':{'fullscreen': {'user': 'John', 'default': False}}}
modifiedouter configuration: {'video': {'fullscreen': {'user': 'John','default': False}}}

希望能更好的解决你的疑惑


在 OP 评论后编辑:正如我公开声明的那样,我花了一些时间才弄清楚发生了什么。让我们来看这段代码:

configuration = {
"video": {
"fullscreen": {
"user": None,
"default": False
}
}
}

print("step 1 -> " + str(configuration))

# correcting global variable blueprint
# configuration["video"]["fullscreen"]["user"] = "John"

class test():

print("step 2 -> " + str(configuration))
user_conf = configuration["video"]["fullscreen"]["user"]

def __init__(self):
# printing modified global variable, all right
print("step 5 -> constructor reads the updated value: " + str(configuration))

def main():
# modifying global variable later
# at this point the class attribute user_conf has already been assigned with the old value
print("step 3 -> " + str(configuration))
configuration["video"]["fullscreen"]["user"] = "John"
print("step 4 -> main just updated the global variable: " + str(configuration))
test()

if __name__ == '__main__':
main()

打印这将为您提供以下输出:

step 1 -> {'video': {'fullscreen': {'user': None, 'default': False}}}
step 2 -> {'video': {'fullscreen': {'user': None, 'default':False}}}
step 3 -> {'video': {'fullscreen': {'user': None,'default': False}}}
step 4 -> main just updated the globalvariable: {'video': {'fullscreen': {'user': 'John', 'default':False}}}
step 5 -> constructor reads the updated value:{'video': {'fullscreen': {'user': 'John', 'default': False}}}

现在,如果您阅读 this answer您将很容易理解 Python 是自上而下执行的,并且执行 def block - 在我们的例子中 __init__(self) - 不会立即执行包含的代码。相反,它在当前作用域中创建一个具有给定名称的函数对象,该函数对象实际上仅在从 main() 调用 test() 之后才进入,即仅在您要求实例之后来自 test() 类的对象,它将触发其构造函数

重要:在您的情况下,我意识到您正在调用类 test() 并且 test() 是您调用的来源主要()。您的 main 实际上是在调用一个方法,test():因此请将之前代码中的 class test() 替换为 def test() 并且您将获得不同且更易于理解的执行流程:

step 1 -> {'video': {'fullscreen': {'user': None, 'default': False}}}
step 3 -> {'video': {'fullscreen': {'user': None, 'default':False}}}
step 4 -> main just updated the global variable:{'video': {'fullscreen': {'user': 'John', 'default': False}}}
step 2 -> {'video': {'fullscreen': {'user': 'John', 'default':False}}}

在第一个 print 之后,所有的 def block 都被跳过,我们进入 main()main() 更新全局变量,然后 test() 函数将立即处理更新后的值。当然,这种情况下的构造函数不会被触发[这不再是一个类],这解释了缺少第 5 步

->您确定以这种方式定义和使用您的类是一个好的选择吗? [可能不是]
-> 将 test() 声明为 def 而不是声明为 class 不是更好吗? [我真的这么认为]

关于python - 类外字典阅读行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53127120/

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