+ 2

Super() with multiple inheritance

Hello, I'm trying to understand how super() works with multiple inheritance. I wrote the following code (https://code.sololearn.com/cCgka5B4tvbR): The class Parrot is subclass of the class Bird and the class Friend. I'm trying to use super() instead of calling class Friend explicitly. How can I use super() when the current class is subclass of more than one parent class. ``` class Friend: def __init__(self, is_close, can_keep_secrets): self.is_close = is_close self.can_keep_secrets = can_keep_secrets class Animal: def __init__(self, name,species): self.name = name self.species = species class Pet(Animal): def __init__(self, name, species, common_name ): super().__init__(name, species) self.common_name = common_name class Bird(Pet): def __init__(self, name, species, common_name, flight_speed): super().__init__(name, species, common_name) self.flight_speed = flight_speed class Parrot(Bird, Friend): def __init__(self, name, species, common_name, flight_speed, color,is_close, can_keep_secrets): super().__init__(name, species, common_name, flight_speed) Friend.__init__(self,is_close, can_keep_secrets) self.color = color parrot1 = Parrot('Parrot','Bird', 'Charlie', '14MPH', 'Red', True, False) print(parrot1.name) print(parrot1.species) print(parrot1.common_name) print(parrot1.flight_speed) print(parrot1.color) print(parrot1.is_close) print(parrot1.can_keep_secrets) ``` Thank you!

17th Aug 2019, 12:03 PM
N. A.
N. A. - avatar
4 Answers
+ 2
In this situation that one of base classes has it's own hierarchy, calling super function makes a route on only the first base class and if you want to call remainder of base classes' __init__, you must call them separately. Btw that's no good practice to assign your attributes via inheritance. It's mainly for using existing attributes and methods in base classes for derived class not defining them.
24th Aug 2019, 9:19 AM
Qasem
+ 1
If you haven't worked it out already :- the 'super' referrers to the first inherited class, so in your example 'Bird' will be the 'super in 'class Parrot(Bird, Friend)' But......, I'm no expert and I'm here to learn.
17th Aug 2019, 5:02 PM
rodwynnejones
rodwynnejones - avatar
+ 1
@Qasem Thank you so much! it was helpful :)
24th Aug 2019, 5:32 PM
N. A.
N. A. - avatar
0
@rodwynnejones Thank you so much :)
24th Aug 2019, 5:30 PM
N. A.
N. A. - avatar