Search code examples
pythonoopabstract-classabc

Python, __init__ method of abstract class implicitly being called?


I'm not too familiar with the use of abstract classes, so I'm trying to understand what is happening in some code I'm using at the moment.

In the code, I have a base dataset class, and some implementations of datasets inheriting from the main one, something like:

class dataset(metaclass=abc.ABCMeta):
    def __init__():
        # implementation of __init__

    @abc.abstractmethod
    def _some_method(self, ....):
        return


class dataset_01(dataset):
    def _some_method(self, ....):
        # implementation of _some_method for dataset_01, overriding abstract method from base class

What I don't understand is, I was expecting to see a call for super().__init__() in dataset_01:

class dataset_01(dataset):
    def __init__(self, length):
        super().__init__(...)

There's no such call, but when debugging the code, I noticed that I still end up in the constructor of dataset when creating an instance of dataset_01, in spite of the missing super.

Is the use of metaclass=abc.ABCMeta in the dataset class leading to some automatic method resolution, i.e. is it ensuring that the __init__ method of the base class is called anyway? Or am I missing something else?


Solution

  • When you create a subclass in Python, if that subclass does not have its own __init__() method, it will automatically inherit the __init__() method from the parent class. That's why, even if you haven't explicitly called super().__init__() in dataset_01, the constructor of the parent class dataset is still invoked when you create an instance of dataset_01.

    The use of metaclass=abc.ABCMeta does not affect this behavior. Its primary purpose is to ensure that the class containing this metaclass cannot be instantiated directly (because it's an abstract class) and that all its abstract methods must be implemented by any non-abstract subclass.

    In summary, the behavior you observe is standard in Python and is not specific to the use of abstract classes.