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
Post a Comment