I’m in the beginning of Ex 42 but I’ve been sidetracked by something in it. I was punching in the code for Ex 42 when I got to the line that had super() in it. I was already somewhat aware of super() having encountered it doing online research on classes but I decided to pause working on 42 to better understand super(). I was reading up on it and found some code as an example. I started playing around with the code to see how I could break it and I found something I cannot explain/understand why it’s happening.
This is the original code:
class Animal:
def __init__(self, Animal):
print(Animal, 'is an animal.')
class Mammal(Animal):
def __init__(self, mammalName):
print(mammalName, 'is a warm-blooded animal.')
super().__init__(mammalName)
class NonWingedMammal(Mammal):
def __init__(self, NonWingedMammal):
print(NonWingedMammal, "can't fly.")
super().__init__(NonWingedMammal)
class NonMarineMammal(Mammal):
def __init__(self, NonMarineMammal):
print(NonMarineMammal, "can't swim.")
super().__init__(NonMarineMammal)
class Dog(NonMarineMammal, NonWingedMammal):
def __init__(self):
print('Dog has 4 legs.')
super().__init__('Dog')
d = Dog()
print('')
print(Dog.__mro__)
print('')
I added the lines about printing the MRO in for reasons I’ll get to in a minute.
This code when run produces the following in PowerShell:
PS C:\Users\dswal\AppData\Local\Programs\Python\Python36\Doug> python testout.py
Dog has 4 legs.
Dog can't swim.
Dog can't fly.
Dog is a warm-blooded animal.
Dog is an animal.
(<class '__main__.Dog'>, <class '__main__.NonMarineMammal'>, <class '__main__.NonWingedMammal'>, <class '__main__.Mammal'>, <class '__main__.Animal'>, <class 'object'>)
While I was messing with the code I moved all the super() lines above the print() lines in each method that had super() in it. So it looked like this…
class Animal:
def __init__(self, Animal):
print(Animal, 'is an animal.')
class Mammal(Animal):
def __init__(self, mammalName):
super().__init__(mammalName)
print(mammalName, 'is a warm-blooded animal.')
class NonWingedMammal(Mammal):
def __init__(self, NonWingedMammal):
super().__init__(NonWingedMammal)
print(NonWingedMammal, "can't fly.")
class NonMarineMammal(Mammal):
def __init__(self, NonMarineMammal):
super().__init__(NonMarineMammal)
print(NonMarineMammal, "can't swim.")
class Dog(NonMarineMammal, NonWingedMammal):
def __init__(self):
super().__init__('Dog')
print('Dog has 4 legs.')
d = Dog()
print('')
print(Dog.__mro__)
print('')
This modification, when run in PowerShell, produces the following:
PS C:\Users\dswal\AppData\Local\Programs\Python\Python36\Doug> python testout.py
Dog is an animal.
Dog is a warm-blooded animal.
Dog can't fly.
Dog can't swim.
Dog has 4 legs.
(<class '__main__.Dog'>, <class '__main__.NonMarineMammal'>, <class '__main__.NonWingedMammal'>, <class '__main__.Mammal'>, <class '__main__.Animal'>, <class 'object'>)
Moving the super() above the print() statement in each method reverses the order of the prints in PowerShell. I’m sure there’s a logical reason for this but what blows my mind is that the print order reverses despite the MRO not changing.
Can someone explain to me why this is happening? I’m not getting it.