gpt4 book ai didi

python - python中困惑的值范围

转载 作者:行者123 更新时间:2023-11-30 22:52:22 25 4
gpt4 key购买 nike

嗨,我一直在用 c 编程,但我不明白 python 中的值范围。

这是我的代码

class ScenarioEnvironment():
def __init__(self):
print(self)

class report():
config = ScenarioEnvironment()
def __init__(self):
self.config = ScenarioEnvironment()

在 init() 中传递 config 和 config 会发生什么?

我想知道哪些配置具有类值(value)?

最佳答案

您需要了解类属性和实例对象属性之间的区别。也许这些代码会对您有所帮助:

class TestConfig1(object):
config = 1

def __init__(self):
self.config = 2


class TestConfig2(object):
config = 1

def __init__(self):
self.config2 = 2

if __name__ == "__main__":
print TestConfig1.config
t = TestConfig1()
print t.config
t2 = TestConfig2()
print t2.config
print t2.config2

更多可以看python博客。 click here

关于python - python中困惑的值范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38602718/

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