類Link
用於構建和操作鏈表:如何在這種情況下正確覆蓋__add__?
class Link:
"""A linked list with a first element and the rest."""
empty =()
def __init__(self, first, rest=empty):
assert rest is Link.empty or isinstance(rest, Link)
self.first = first
self.rest = rest
def __getitem__(self, i):
if i == 0:
return self.first
else:
return self.rest[i-1]
def __len__(self):
return 1 + len(self.rest)
def __repr__(self):
"""Return a string that would evaluate to self."""
if self.rest is Link.empty:
rest = ''
else:
rest = ', ' + repr(self.rest)
return 'Link({0}{1})'.format(self.first, rest)
通過模仿__repr__
我想要實現的功能__add__
:
def __add__(self, other):
if self is Link.empty:
return other
else:
return Link(self.first, add(self.rest, other))
但是它不工作,並給了我這樣的錯誤(實際的文件路徑是隱藏的):
>>> lst = Link(3, Link(4, Link(5)))
>>> lst
Link(3, Link(4, Link(5)))
>>> lst + lst
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "PATH_TO_THE_FILE", line 31, in __add__
return Link(self.first, add(self.rest, other))
NameError: name 'add' is not defined
所以我改變了的最後一行到:
return Link(self.first, self.rest.__add__(other))
和此時的誤差變爲:
TypeError: can only concatenate tuple (not "Link") to tuple
然後我在類刪除__add__
方法並試圖另一種方式:
第一添加以下功能的模塊:
def extend_link(s, t):
if s is Link.empty:
return t
else:
return Link(s.first, extend_link(s.rest, t))
並在終端:
>>> lst = Link(3, Link(4, Link(5)))
>>> Link.__add__ = extend_link
>>> lst + lst
Link(3, Link(4, Link(5, Link(3, Link(4, Link(5))))))
那麼,爲什麼Link.__add__ = extend_link
的作品,但首要__add__
在班上沒有?
PS:這個例子是從here
什麼是「添加」?這顯然是抱怨缺少「添加」功能。 –