擅长:python、mysql、java
<p>您只需从<code>tuple</code>继承即可,它保留了所有所需的行为。如果您想添加一个属性,就不能在<code>__init__</code>中添加,因为实例已经存在,并且是不可变的!因此,您必须重写<code>__new__</code>,它实际上创建了实例:</p>
<pre><code>class Supertuple(tuple):
def __new__(cls, arg):
self = super(Supertuple, cls).__new__(cls, arg)
self._tuple = tuple(arg)
return self
>>> s = Supertuple([1,2,3])
>>> s._tuple
(1, 2, 3)
>>> s
(1, 2, 3)
</code></pre>