Skip to content
Advertisement

Multiple Python Inheritance with abstract class calls __new__ method wrong number of arguments

I have the following situation:

JavaScript

But when I want to generate a new Person I get an TypeError:

JavaScript

Now I get that it has something to do with the fact that I have implemented a new method in BasicGreeter because when I rewrite BasicGreeter to use an init method instead of the new-method and then explicitly call super.init() in the Person’s init it works. But I wanted to know if there is any way to do this with the new method as having to invoke super.init() in every Speakers init can be a bug source if ever forgotten. Any ideas, can this (a class implementing basic compliance in some areas for an abstract class so that children of that abstract class don’t need to implement the same code) be done? I know that I could always go the 3 generation route making the BasicGreeter an abstract class, but I think that is more elegant way as it allows BasicGreeter to be used for other families f.e. I might have a class Robot with a subclass FriendlyRobot that could also make use of inheriting from BasicGreeter but a Robot is not necessarily a speaker and vice versa.

Advertisement

Answer

The error gives you the solution, simply let the new method to take the arguments even if you don’t use them:

JavaScript

This piece of code works, said that, I’m not sure in what you need such a structure inside new, is a way of making a singleton?

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement