3

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?

1
  • 2
    You might want to be careful passing lists as the defaults the __init__ like that. That's just begging for interesting behavior ... stackoverflow.com/questions/1132941/… Commented Jan 9, 2013 at 16:53

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Could you comment on why this is? I've always wondered. It seems as though the derived class should be trivially easy to identify automatically by the interpreter. Is there a use case for using something different as the first parameter?
Note that in 3.x, super() can take no arguments and work fine.
@acjohnson55: The MRO of the base classes depend on the inheriting class. Imagine a class that inherits from two super classes.
So, you're saying that you can use a base class in super, but then a compatible method better be in the base class's superclass lineage someplace?
@acjohnson55: Exactly. And it might be the wrong one, if there was a different MRO path to follow based on your current class.
|
2

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.