What's the difference and problems involved in this curiosity:
class A(object):
def __init__(self):
super(A, self).__init__()
Than
class A(object):
def __init__(self):
pass
class B(A):
def __init__(self):
super(B, self).__init__()
Even if the first example is wrong, it works. I thought it could be a redundancy, but I heard that using super() in a class that's inherited from object is wrong, but why?
Using super(A, self).__init__()
is just fine; object
has an __init__
method too.
What would not work is trying to pass arguments to that method; object.__init__()
takes no parameters. And for most methods other than __init__
, super()
in a class derived directly from object
may also not work because object
does not have the specific method you are trying to call.
Thus, using super(class, self).__init__()
with anything other than an empty argument list requires more intimate knowledge of your class hierarchy, where any classes deriving directly from object
should take care not to pass on arguments.
For any custom methods, super(class, self).other_method
will most likely fail because object
simply doesn't implement that method.
super(class. self) is how one interacts with what's called the MRO, or method resolution order.
A very important concept to grok. Here's Guido on MRO: http://python-history.blogspot.com/2010/06/method-resolution-order.html?m=1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.