擅长:python、mysql、java
<p>我将通过重载您想要支持的某些操作符来实现这一点,例如<a href="https://docs.python.org/3/reference/datamodel.html#object.__iadd__" rel="nofollow">+= operator by defining ^{<cd1>}</a>。然后,您可以执行以下操作:</p>
<pre><code>class C(object):
def __init__(self, values):
self._values = values
def __iadd__(self, value):
self._values += " - " + str(value)
return self
obj = C("one - two")
obj += "three"
print(obj.values) # "one - two - three"
</code></pre>
<p>有关运算符重载的详细信息,请参见<a href="https://docs.python.org/3/reference/datamodel.html#object.__iadd__" rel="nofollow">docs</a>。你知道吗</p>