gpt4 book ai didi

python - 是什么让用户定义的对象不会在 Python 中抛出 AttributeError?

转载 作者:太空狗 更新时间:2023-10-30 01:30:00 24 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
Why can't I directly add attributes to any python object?
Why can't you add attributes to object in python?

下面的代码不会抛出AttributeError

class MyClass():
def __init__(self):
self.a = 'A'
self.b = 'B'
my_obj = MyClass()
my_obj.c = 'C'

对比

>>> {}.a = 'A'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'a'

是什么造成了这种差异?是关于 dict 是内置类而 MyClass 是用户定义的吗?

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