Reputation: 11774
I'm trying to use super() for a simple class hierarchy in this manner:
class Employee(object):
def __init__(self, name):
self.name = name
class FullTime(Employee):
def __init__(self, name, satOff, freeDays=[], alDays=[], programDays=[]):
global satsOffDict
global dayDict
super(Employee, self).__init__(name)
However, I'm getting this error:
TypeError: object.__init__() takes no parameters
I've read that you need to make the parent object type object (new style classes) in order for super to work. If I change class Employee(object) to class Employee(), I get this error:
TypeError: must be type, not classobj
What's going on?
Upvotes: 3
Views: 675
Reputation: 1121554
You use the current class in super()
:
super(FullTime, self).__init__(name)
super()
looks for requested methods relative to the first argument; you started the search from Employee
instead, looking for the parent classes; object.__init__()
is the next parent method that matches in that case.
You need to use the current class, because Python needs to search the base classes in Method Resolution Order; that ordering depends on how your current class is derived from it's base classes (especially if there is multiple inheritence or a base class ends up being referenced multiple times).
On Python 3.x, you can use super()
without any arguments, and the compiler will add an extra hint to your method so that it can determine the correct class to start from.
Upvotes: 7
Reputation: 46183
When you use super
in Python, you should be specifying the derived type (that is, the class in which you're writing the call) as the first parameter.
super(FullTime, self).__init__(name)
Upvotes: 2