The abstract methods of python's abc module make no distinction between classes and instances.

Python didn't have an abstract class or interface. However, the abc (abbreviation for abstract base class) module has been added, and it has become possible to achieve this by using a decorator. This time, let's experiment to find out the conditions under which the abstract class actually gives an error.

Abstract class gives an error when instantiating

An abstract class enforces a function (method) that you want to implement. If you don't implement it in python, you will get an error only when you instantiate an implemented class (more precisely, an unimplemented class). Therefore, even if the class does not implement the abstract method, it will not cause an error if you just access it to use the class method. In the following cases, the error occurs only when instantiating the abstract class and its subclasses.

from abc import *

class Abstract(object, metaclass=ABCMeta):

    @classmethod
    @abstractmethod
    def hello(cls):
        print("Abstract hello")

class Implement(Abstract):
    pass

Abstract.hello()
# => Abstract hello
Implement.hello()
# => Abstract hello

# Abstract()
# TypeError: Can't instantiate abstract class Abstract with abstract methods hello

# Implement()
# TypeError: Can't instantiate abstract class Implement with abstract methods hello

Of course, when instantiating, you have to implement (define) an abstract method. Even if you create another method, it is not considered to be implemented. In other words, it does not meet the criteria of the creator of an abstract class. Let's check it.

from abc import *

class Abstract(object, metaclass=ABCMeta):

    @classmethod
    @abstractmethod
    def hello(cls):
        print("Abstract hello")

class Implement(Abstract):
    @classmethod
    def say(cls):
        print("Implement class-method say")

    def yeah(self):
        print("Implement instance-method yeah")

Implement()

# => TypeError: Can't instantiate abstract class Implement with abstract methods hello

No distinction is made between abstract class methods and instance methods

Define a class method of an abstract class as an abstract method. If you define a method with the same name as an instance method in that subclass, no error will occur. Perhaps python is all objects and indistinguishable, so it only determines the name, not the method type.

from abc import *

class Abstract(object, metaclass=ABCMeta):

    @classmethod
    @abstractmethod
    def hello(cls):
        print("Abstract hello")

class Implement(Abstract):
    def hello(self):
        print("Implement hello")

impl = Implement()
impl.hello()

# => Implement hello

Let's define an abstract class method as an instance method and an abstract instance method as a class method. The error this time is due to the absence of the class method hello. Since the subclass hello is an instance and the parent class hello is an abstract method, the class method will result in an error because hello is not implemented. It doesn't cause an error to access the class, but it says it's useless because there is no method instructed to use it.

from abc import *

class Abstract(object, metaclass=ABCMeta):

    @classmethod
    @abstractmethod
    def hello(cls):
        print("Abstract hello")

    def say(self):
        print("Abstract say")

class Implement(Abstract):

    def hello(self):
        print("Implement hello")

    @classmethod
    def say(cls):
        print("Implement say")

impl = Implement()
impl.hello()
impl.say()
# =>
# Implement hello
# Implement say

# Implement.hello()
# hello() missing 1 required positional argument: 'self'

Implement.say()

# =>
# Implement say

How far can you force the implementation

In this experiment, we found that it is not possible to distinguish between class methods and instance methods. You may have prepared the error yourself, or you may have a method in the abc module options. However, keep in mind that normal usage can only limit the implementation of methods with the same name.

Recommended Posts

The abstract methods of python's abc module make no distinction between classes and instances.
Python classes and instances, instance methods
Bind methods to Python classes and instances
Summary of the differences between PHP and Python
The answer of "1/2" is different between python2 and 3
Consideration of the difference between ROC curve and PR curve
[python] Get the list of classes defined in the module
Visualization of the connection between malware and the callback server