Different Sets of Arguments
Learn how to manage different sets of arguments in Python.
Manage different sets of arguments
This is going to make things complicated as we return to our Friend
cooperative multiple inheritance example. In the __init__()
method for the Friend
class, we were originally delegating initialization to the __init__()
methods of both parent classes, with different sets of arguments:
Contact.__init__(self, name, email)
AddressHolder.__init__(self, street, city, state, code)
How can we manage different sets of arguments when using super()
? We only really have access to the next class in the MRO sequence. Because of this, we need a way to pass the extra arguments through the constructors so that subsequent calls to super()
, from other mixin classes, receive the right arguments.
It works like this. The first call to super()
provides arguments to the first class of the MRO, passing the name
and email
arguments to Contact.__init__()
. Then, when Contact.__init__()
calls super()
, it needs to be able to pass the address-related arguments to the method of the next class in the MRO, which is AddressHolder.__init__()
.
This problem often ...