gpt4 book ai didi

python - __dict__ 不知道类的某些属性

转载 作者:太空狗 更新时间:2023-10-30 02:37:40 25 4
gpt4 key购买 nike

<分区>

如果我上课

class MyClass:
"""A simple example class"""
i = 12345

def f(self):
return 'hello world'

然后在控制台中,说

x=MyClass()
x.counter = 1
vars(x) # returns {'counter': 1}
x.__dict__ # returns {'counter': 1}

为什么它不知道 x 的 i 属性?我敢肯定这是因为我对 Python 中类的各种属性的定义方式存在误解。

Python 2/3 在这方面有什么区别吗?调用 dir(x) 会得到我期望的所有内容。

我怀疑这个问题已经被其他初学者问过,但在这个阶段我不知道在问题标题中寻找正确的东西。该网站建议的所有内容似乎都不正确。

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