0

这是一个例子。

>>> class MyList(list):
>>>    def __sub__(self, other):
>>>        L = self[:]
>>>        for x in other:
>>>            if x in L: L.remove(x)
>>>        return L

>>> L = MyList([1, 2, 3, 'spam', 4, 5])
>>> L = L - ['spam']
>>> print L
[1, 2, 3, 4, 5]

当类接受参数时,它需要init,构造函数来获取。但是上面没有init方法。怎么可能?

提前致谢 :)

4

1 回答 1

2

子类化时,您继承父类的方法,除非您在子类定义中覆盖它们。因此,您的代码正在使用基列表类的 __init__() 函数。

于 2013-08-01T01:08:08.043 回答