python - why not using constructor when making subclass of list class? -


here example.

>>> class mylist(list): >>>    def __sub__(self, other): >>>        l = self[:] >>>        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, requires init, constructors get. there no init method above. how possible?

thanks in advance :)

when subclass, inherit methods of parent class unless override them in subclass definition. so, code using __init__() function of base list class.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -