I have two questions regarding the use of super() in python3.
-
Consider the following class definition from ex42
class Employee(Person): def __init__(self, name, salary): # the following two do the same # super(Employee, self).__init__(name) super().__init__(name) ## Employee has-a salary self.salary = salary
As of the python documentation here the two following lines of code do the same thing
super(Employee, self).__init__(name)
super().__init__(name)
I tried it and at least it seems to work the same way.
My question is then, shouldn’t we use the second option
super().__init__(name)
in order not to violate the D.R.Y Don’t repeat yourself principle see here ?
- My second question is that I read in the article above that the new syntax
super()
is equivalent tosuper(__class__, <firstarg>)
where__class__
is the class where the method was defined in. But in our example above this would be equivalent tosuper(Person, self)
which contradicts what I wrote under 1. Can anyone clarify this?
Thanks