小编Hyd*_*S M的帖子

将成员变量添加到python列表对象

在python 3中,您可以在类声明后将成员变量添加到自定义对象:

class a():
    pass

b = a()
b.c = 1 #okay
print(b.c) #outputs 1
Run Code Online (Sandbox Code Playgroud)

但是,对列表对象执行相同操作会引发异常:

d = []
d.e = 1 #throws AttributeError: 'list' object has no attribute 'e'
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么会这样吗?

python attributes python-3.x

4
推荐指数
1
解决办法
267
查看次数

标签 统计

attributes ×1

python ×1

python-3.x ×1