I came across this code in Object Oriented Programming by Dusty Phillips (simplified for brevity) and I unsure about a specific part of this definition.
class A:
def __init__(self, a, **kwargs):
super().__init__(**kwargs)
self.a = a
class B:
def __init__(self, b, **kwargs):
super().__init__(**kwargs)
self.b = b
class C(A, B):
def __init__(self, c, **kwargs):
super().__init__(**kwargs)
self.c = c
(__main__.C, __main__.A, __main__.B, object)
, could class B
be defined in the following way instead?class B:
def __init__(self, b):
self.b = b
super().__init__(**kwargs)
in class B
redundant, since any surplus kwargs
passed to C
will be passed to object
, raising?TypeError: object.__init__() takes exactly one argument (the instance to initialize)
C
was defined as class C(B, A)
instead of class C(A, B)
?Consider how you might instantiate C
:
c = C(a=3, b=5, c=9)
C.__init__
gets all the keyword arguments, but only uses the one for its own parameter c
. The rest are passed on for the next __init__
method in the chain. In this case, that's A.__init__
, which "pulls out" the argument for a
and passes b
on to B.__init__
. B
uses that and passes on the (now-empty) set of keyword arguments to the next method, object.__init__
. Because all keyword arguments have been "claimed" and processed by other classes, object.__init__
succeeds.
Because of how the MRO is constructed, classes that properly use super()
guarantee collectively that **kwargs
will be empty by the time object.__init__
is called.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With