例如:
class Foo:
def __init__(self):
self.bar = ["baz", "qux", "quux", "quuz", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"]
如何调用附加到Foo().bar
的Foo().append()
例如:
x = Foo()
x.append("asd")
# What I want to happen:
# self.bar now is [..., "asd"]
# What actually happens:
# AttributeError: 'Foo' object has no attribute 'append'
这可能吗
我自己添加了一个
append
函数:编辑:一个更简单的方法,也可以工作
(感谢@RaySteam)
您可以在
__init__
函数中设置self.append
:然后按预期调用:
如果这是我们的目标,为什么不只是子类
list
相关问题 更多 >
编程相关推荐