Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python ABC & Multiple Inheritance

I would like to know if it is possible to use multiple inheritance with abstract base class in python. It seems like it should be possible but can't find a statement one way or the other.

The basic ABC example:


from abc import ABC, abstractmethod


class BaseABC(ABC):

    @abstractmethod
    def hello(self):
        pass


class Child(BaseABC):
    pass


child = Child()

This will fail due to "hello" not being implemented in "Child".

What I would like is to know how to combine ABC with multiple inheritance. I would like to make either the "BaseABC" or "Child" to inherit also from some other separate class. Explicitly:


from abc import ABC, abstractmethod


class BaseABC(ABC, dict):

    @abstractmethod
    def hello(self):
        pass


class Child(BaseABC):
    pass


child = Child()

This does not fail in the way expected as the first case does. Also:


from abc import ABC, abstractmethod


class BaseABC(ABC):

    @abstractmethod
    def hello(self):
        pass


class Child(BaseABC, dict):
    pass


child = Child()

This does not fail either. How can I require "Child" to implement "hello"?

like image 419
kerzane Avatar asked Feb 12 '21 14:02

kerzane


People also ask

What is an ABC in Python?

The 'abc' module in Python library provides the infrastructure for defining custom abstract base classes. 'abc' works by marking methods of the base class as abstract. This is done by @absttractmethod decorator.

What does ABC Abstractmethod do?

abc. abstractmethod(function) A decorator indicating abstract methods. Using this decorator requires that the class's metaclass is ABCMeta or is derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods and properties are overridden.

What is the difference between ABC and ABCMeta Python?

ABCMeta . i.e abc. ABC implicitly defines the metaclass for us. The only difference is that in the former case you need a simple inheritance and in the latter you need to specify the metaclass.

What is Abstractproperty Python?

Abstract Properties Properties are Pythonic ways of using getters and setters. The abc module has a @abstractproperty decorator to use abstract properties. Just like abstract methods, we need to define abstract properties in implementation classes, otherwise, Python will raise the error.


Video Answer


1 Answers

The issue is with inheriting from a dict, which is probably better explained by these guys:

  • https://treyhunner.com/2019/04/why-you-shouldnt-inherit-from-list-and-dict-in-python/
  • http://www.kr41.net/2016/03-23-dont_inherit_python_builtin_dict_type.html

So depending on little what you want to do with your subclassed dict you could go with MutableMapping as suggested in https://stackoverflow.com/a/3387975/14536215 or with UserDict (which is a subclass of MutableMapping) such as:

from abc import ABC, abstractmethod
from collections import UserDict


class BaseABC(ABC):

    @abstractmethod
    def hello(self):
        pass

class Child(BaseABC, UserDict):
    pass


child = Child()
like image 107
Tzane Avatar answered Oct 22 '22 00:10

Tzane