使用附加对象调用类,该附加对象以类变量为目标

问题描述 投票:1回答:1

例如:

class Foo:
    def __init__(self):
        self.bar = ["baz", "qux", "quux", "quuz", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"]

如何调用附加在Foo().append()上的Foo().bar

Ex:

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'

这可能吗?

python python-3.x
1个回答
1
投票

我自己添加了append函数:

# ... in the Foo() class

    def append(self, value):
        return self.bar.append(value)
© www.soinside.com 2019 - 2024. All rights reserved.