Вопрос

Here is an example.

>>> 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]

When class takes arguments, it requires init, constructors to get. But there is no init method above. How could it be possible?

Thanks in advance :)

Это было полезно?

Решение

When you subclass, you inherit the methods of the parent class unless you override them in the subclass definition. So, your code is using the __init__() function of the base list class.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top