[英]why not using constructor when making subclass of list class?
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. 当类接受参数时,它需要init ,构造函数来获取。 But there is no init method above. 但是上面没有init方法。 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. 因此,您的代码使用基类列表类的__init __()函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.