擅长:python、mysql、java
<p>您可以在<code>__init__</code>函数中设置<code>self.append</code>:</p>
<pre><code>class Foo:
def __init__(self):
self.bar = ["baz", "qux", "quux", "quuz", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"]
self.append = self.bar.append
</code></pre>
<p>然后按预期调用:</p>
<pre><code>x = Foo()
x.append("asd")
print(x.bar)
#[..., "asd"]
</code></pre>
<p>如果这是我们的目标,为什么不只是子类<code>list</code></p>
<pre><code>class Foo(list):
def __init__(self):
self.extend(["baz", "qux", "quux", "quuz", "corge", "grault", "garply", "waldo", "fred", "plugh", "xyzzy", "thud"])
f = Foo()
f.append('asd')
print(f)
#[..., "asd"]
</code></pre>