Having a language causes the superclass to initialize before or after, which means you are losing functionality. A subclass may depend on the initialization of the superclass that should be launched first, or vice versa.
In addition, he would have no way of knowing which arguments to pass - the subclasses decide which values ββare passed to the initializer, which gives more flexibility when automatically passing all arguments.
An alternative method to initialize a superclass is to use super , although it will actually initialize the superclass of the self
class dynamically, by searching for it in the __mro__
object (method resolution order)
In python 2:
super(self, Dog).__init__(self, name, "Dog")
In python 3, you can further reduce the syntax and not repeat yourself:
super().__init__(self, name, "Dog")
Edit
Since you are not actually using the name
argument passed to the dog, you can further reduce some syntax by accepting an arbitrary keyword and positional arguments in the Dog
initializer and passing them the initialization chain:
class Dog(Pet): def __init__(self, chases_cats, *args, **kwargs): Pet.__init__(self, *args, species="Dog", **kwargs) self.chases_cats = chases_cats class Pet(object): def __init__(self, name, species, *args, **kwargs): self.name = name self.species = species
In your scenario, the initializer may be simple enough to not be needed, but it is useful to know when you have more arguments and / or a deeper class hierarchy.