Is it possible to make abstract classes in Python?

How can I make a class or method abstract in Python?

I tried redefining __new__() like so:

class F:
    def __new__(cls):
        raise Exception("Unable to create an instance of abstract class %s" %cls)

but now if I create a class G that inherits from F like so:

class G(F):
    pass

then I can’t instantiate G either, since it calls its super class’s __new__ method.

Is there a better way to define an abstract class?

13 Answers
13

Leave a Comment