2 回答
TA贡献1877条经验 获得超1个赞
class Checkbook:
def __init__(self):
self.transactions = [] # Checkbook is just a wrapper around a list
def __getitem__(self, index): # implementing [x]
return self.transactions[index]
def __setitem__(self, index, transaction): #implementing [x] = i
self.transactions[index] = transaction
def append(self, transaction): # implementing .append(x)
self.transactions.append(transaction)
def extend(self, transaction_list): # implementing .extend([x,y,z])
self.transactions.extend(transaction_list)
# and so on for every method which you want to support
或者,您可以继承list。
这是做作业吗?在Python中分配它是很愚蠢的。您永远不会真正想要继承list。在大学里,我有一个更好的练习,在该练习中,我必须重新std::vector使用模板,动态数组(带有malloc())和运算符重载……,这真有趣!该std::map演习是更好:)
添加回答
举报