The SomeClass class has a custom metaclass. You will need to create a metaclass which inherits from both ABCMeta and this custom metaclass, then use it as the metaclass for MyClass. Without knowing more about this custom metaclass, I cannot determine a correct way to do this in the general case, but it will probably look like one of these possibilities:

class DerivedMeta(ABCMeta, type(SomeClass)):
    pass

class DerivedMeta(type(SomeClass), ABCMeta):
    pass

It's unlikely but possible you will also need to override one or more methods to ensure correct metaclass interactions.

Answer from Kevin on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ abc.html
abc โ€” Abstract Base Classes
This implementation can be called via the super() mechanism from the class that overrides it. This could be useful as an end-point for a super-call in a framework that uses cooperative multiple-inheritance. The abc module also supports the following legacy decorators:
๐ŸŒ
Medium
elfi-y.medium.com โ€บ inheritance-in-python-with-abstract-base-class-abc-5e3b8e910e5e
Inheritance in Python with Abstract Base Class(ABC) | by E.Y. | Medium
January 13, 2021 - Also, itโ€™s just very difficult to manage inheritance relationship without proper subclassing anyway. For example, a Member and SupermarketStock class can both can len(). But they are not the same thing even if they implement the same methods. And thatโ€™s where Abstract Base Classes(ABC) come to rescue.
๐ŸŒ
Pybites
pybit.es โ€บ articles โ€บ elevate-your-python-harnessing-the-power-of-abstract-base-classes-abcs
Elevate Your Python: Harnessing The Power Of Abstract Base Classes (ABCs) - Pybites
February 21, 2024 - However, Python provides a more ... from ABC requires the usual precautions regarding metaclass usage, as multiple inheritance may lead to ......
๐ŸŒ
Medium
medium.com โ€บ @pouyahallaj โ€บ introduction-1616b3a4a637
Python Protocols vs. ABCs: A Comprehensive Comparison of Interface Design | Medium
May 29, 2023 - ABC methods are defined using the @abstractmethod decorator and must be implemented by subclasses. Subclasses inherit the structure and behavior of the ABC, enforcing adherence to the defined interface.
Find elsewhere
๐ŸŒ
Edube
edube.org โ€บ learn โ€บ python-advanced-1 โ€บ abstract-classes-vs-method-overriding-multiple-inheritance
Edube Interactive :: 2.6.1.7 Abstract classes vs. method overriding โ€“ multiple inheritance
When you plan to implement a multiple inheritance from abstract classes, remember that an effective subclass should override all abstract methods inherited from its super classes. Summary: Abstract Base Class (ABC) is a class that cannot be instantiated. Such a class is a base class for concrete ...
Top answer
1 of 3
5

Using Python's multiple inheritance (MI) for interfaces, abstract base classes, mixins, or similar techniques is perfectly fine. In most cases, the MRO produces intuitive results.

However, object initialization under multiple inheritance is really tricky. In Python you cannot combine multiple classes per MI unless all participating classes have been designed for MI. The issue is that the __init__() method cannot know from which class it will be called and what the signature of the super().__init__() method will be. Effectively, this means that MI constructors:

  • must call the super().__init__()
  • must only take arguments by name, not by position
  • must forward any **kwargs to the super().__init__()
  • must not warn on unexpected arguments

Where possible, the better alternative is to avoid __init__() methods for interface-like classes, and instead express requirements through abstract methods. For example, instead of a BananaContainer class, we might write this interface/ABC:

import abc  # abstract base class

class BananaContainer(abc.ABC):
  @property
  @abc.abstractmethod
  def bananas(self) -> list:
    raise NotImplementedError

If a class wants to be a BananaContainer, it would have to implement that property.

In general, it is perfectly alright if you have a class that inherits from multiple interfaces or mixins. Aside from name collisions, the above __init__() problems, and general API bloat of the class, no noteworthy issues arise.


The second part of your question proposes a capability-based approach instead of using inheritance. Using composition instead of inheritance is often a very very good idea. For example, you eliminate the initialization problems by design. It also tends to lead to more explicit APIs that are easier to navigate and avoid name clashes. There should be some method that either returns an object representing a capability, or None if the capability isn't supported.

But these capabilities can be implemented in different ways: either by using normal composition, or by storing the capabilities in your own data structures.

  • Unless you have special needs for the object model, stick to the language. Store methods in normal object fields, provide normal methods to access them. This leads to a more comfortable API, and is more likely to support auto-completer and type-checkers.

  • If you need to modify the available capabilities of an object at run-time, and need to introduce new kinds of capabilities at run-time, then using a dictionary may be appropriate. But at this point you are inventing your own object system. This may be a good idea e.g. in games that have complex capability systems where new capabilities shall be defined in configuration files.

    Most software does not have these requirements, and does not benefit from that kind of flexibility.

    Additionally, Python's built-in object system is flexible enough that you could create new types and new methods without having to create a new object system. Builtins like getattr(), setattr(), hasattr(), and the type() constructor come in handy here.

I would likely express an object that can have both AppleContainer and BananaContainer capabilities like this:

class BananaContainer:
  ...

class AppleContainer:
  ...

class HasCapabilities:
  def __init__(self, x, y, z):
    # somehow determine the appropriate capabilities and initialize them
    self._banana_container = BananaContainer(y) if x else None
    self._apple_container = AppleContainer(y)

  @property
  def as_banana_container(self) -> Optional[BananaContainer]:
    return self._banana_container

  @property
  def as_apple_container(self) -> Optional[AppleContainer]:
    return self._apple_container

o = HasCapabilities(...)
bc = o.as_banana_container
if bc is not None:
  bc.do_banana_things()

Or with Python 3.8 assignment expressions:

if (bc := o.as_banana_container) is not None:
  bc.do_banana_things()

If you want to have some custom mechanisms for reflection over capabilities, you can implement that on top of this solution, with some amount of boilerplate. If we want to be MI-safe, we might declare the following base class that all capability-having classes need to inherit:

class CapabilityReflection:
  # a base implementations so that actual implementations
  # can safely call super()._get_capabilities()
  def _list_capabilities(self):
    return ()

  def all_capabilities(self):
    """deduplicated set of capabilities that this object supports."""
    set(self._list_capabilities())

  def get_capability(self, captype):
    """find a capability by its type. Returns None if not supported."""
    return None

which in the above case would have been implemented as:

class HasCapabilities(CapabilityReflection):
  ...
  def _list_capabilities(self):
    caps = [  # go through properties in case they have been overridden
      self.as_banana_container,
      self.as_apple_container,
    ]
    yield from (cap for cap in caps if cap is not None)
    yield from super()._list_capabilities()

  def get_capability(self, captype):
    if captype == BananaContainer:
      return self.as_banana_container
    if captype == AppleContainer:
      return self.as_apple_container
    return super().get_capability(captype)
2 of 3
2

In order to ensure a class has some properties, I make base "interface" classes

While this is a common design pattern in statically typed languages, Python programmers consider more idiomatic to use duck typing for your classes. Since the language is dynamically typed, if you have Foo and Bar classes that both can contain bananas, you are free to call unknown.banana on a variable that can be either. If unknown can be an object that don't implement banana, you can also use getattr or try/except AttributeError blocks. The explicit interface is just bloat over features the language already support.

If for any reason you don't want to get rid of these interfaces, then you could at least use multiple inheritance. It exists because it has uses and is correct to use in many cases.

can it hit us back later with problems like method resolution order or name collisions ?

In your multiple inheritance declaration, the first object has priority when it comes to symbol collisions. In some cases, it's a feature, but you have to be careful this doesn't cause unintended overrides, like you would when defining methods and properties in a child class.

The capability suggestion is overly defensive over inheritance mechanisms. Making sure you don't accidentally override is your responsibility, but it shouldn't be a huge burden. If it happens to be one, it's likely you have other problems. And in cases you are not sure of the symbols contained in a class and want to use it as a black box, it may be appropriate to favor composition.

๐ŸŒ
GitHub
github.com โ€บ python โ€บ cpython โ€บ issues โ€บ 104797
Cannot use multiple inheritance with `collections.abc.Buffer` and `typing.Protocol` ยท Issue #104797 ยท python/cpython
March 2, 2023 - Examples include contextlib.AbstractContextManager and collections.abc.Iterable. These classes are special-cased in typing.py to allow for multiple inheritance with typing.Protocol, so that the interface can be extended:
Published ย  May 23, 2023
Author ย  AlexWaygood
๐ŸŒ
Python Course
python-course.eu โ€บ oop โ€บ the-abc-of-abstract-base-classes.php
20. The 'ABC' of Abstract Base Classes | OOP | python-course.eu
Like in other cases of "normal" inheritance, the abstract method can be invoked with super() call mechanism. This enables providing some basic functionality in the abstract method, which can be enriched by the subclass implementation. from abc import ABC, abstractmethod class AbstractClassExample(ABC): @abstractmethod def do_something(self): print("Some implementation!") class AnotherSubclass(AbstractClassExample): def do_something(self): super().do_something() print("The enrichment from AnotherSubclass") x = AnotherSubclass() x.do_something()
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ multiple inheritance
Python Language Tutorial => Multiple Inheritance
Another powerful feature in inheritance ... init method of class, when every class has own init method then we try for multiple ineritance then only init method get called of class which is inherit first....
Top answer
1 of 1
1

There are generally many good ways to implement this. However, everyone have their own preferences, so mostly which of the following you use is up to you. Here's the three approaches:

  1. Do it the way you are currently implementing it. For a basic structure, it'll be something similar to this:
from abc import ABCMeta

class Abstract(metaclass=ABCMeta):
    def write(self):
        raise NotImplementedError()

class Mixin():
    def meth1(self, *args, **kwargs):
        """ do something here"""

class ActualUsefulImplementedClass(Abstract, Mixin):
    """ write your methods here. You will have access to all the helper methods of the mix-in class."""
  1. Implementing the methods as functions (presumably in another file in the same directory as the current file) and call them from inside the class will also be OK:
from abc import ABCMeta
from helpers import func1, func2, func3, ...

class Abstract(metaclass=ABCMeta):
    def write(self):
        raise NotImplementedError()

class ActualUsefulImplementedClass(Abstract, Mixin):
    """ write your methods here. Access the helper functions imported above directly in your methods."""
    # for example
    def do_something(self, *args, **kwargs):
        func1(*args)

This approach does have it's drawbacks, namely:

  • If by any chance your helper methods acts on an instance of some self-defined class, then this approach will break. Of course, you can pass-in the instance as an argument, but that is a very, very bad idea in terms of software engineering ideas and principles.
  • Also, if you want to inherit them once and have access to them without additional code for all of the ActualUsefulImplementedClass's subclasses, then this approach doesn't fit the bill.
  • Finally, if you want a strictly OOP approach, or don't want the helper functions to pollute your current module scope's namespace, then this isn't the best fit as well.
  1. Similar to the first approach stated above (number 1), but create another middle-level class that doesn't use the mix-in. It will be something like the following:
from abc import ABCMeta

class Abstract(metaclass=ABCMeta):
    def write(self):
        raise NotImplementedError()

class Mixin():
    def meth1(self, *args, **kwargs):
        """ do something here"""

class MiddleMan(Abstract):
    """ write your methods here. You will not have access to the helper methods of the mix-in class."""

class ActualUsefulImplementedClass(MiddleMan, Mixin):
    """ You will have both the actual method implementations and the helper methods here without much or any code."""

However, the above code will only work if you don't need the helper methods inside your methods' implementations and only need them outside of the class. If you need them inside the methods' code, then use the first approach. If you only need the helpers outside of the class, then use this approach.


Note: I use docstrings in the line immediately after the class or method definition line whenever applicable. It describes some of the abilities and limitations of the approach, so please read them as well.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ multiple-inheritance-in-python
Multiple Inheritance in Python - GeeksforGeeks
The code demonstrates multiple inheritance where Class4 inherits from Class2 and Class3; calling obj.m() executes Class2โ€™s method because, according to Pythonโ€™s MRO, Class2 is checked before Class3.
Published ย  December 27, 2025
Top answer
1 of 2
27

Unless you use abc.ABCMeta as the metaclass for your class (either explicitly or by inheriting from abc.ABC), using abstractmethod doesn't really do anything.

Copy>>> from abc import abstractmethod, ABC
>>> class Foo:
...   @abstractmethod
...   def bar(self):
...     pass
...
>>> f = Foo()
>>>

Likewise, using ABCMeta doesn't mean much unless you mark at least one method as abstract:

Copy>>> class Bar(ABC):
...     pass
...
>>> b = Bar()
>>>

It's the combination of the two that allows a class to be (nominally) uninstantiable:

Copy>>> class Baz(ABC):
...   @abstractmethod
...   def m(self):
...     pass
...
>>> b = Baz()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Baz with abstract methods m
>>>

(Even then, note that all @abstractmethod does is add the decorated method to a set which the metaclass machinery consults when trying to instantiate the class. It is trivial to defeat that machinery:

Copy>>> Baz.__abstractmethods__
frozenset({'m'})
>>> Baz.__abstractmethods__ = set()
>>> b = Baz()
>>>

)


Note that ABC itself is a trivial class that uses ABCMeta as its metaclass, which makes any of its descendants use it as well.

Copy# Docstring omitted; see
# https://github.com/python/cpython/blob/3.7/Lib/abc.py#L166
# for the original
class ABC(metaclass=ABCMeta):
    __slots__ = ()
2 of 2
0

What chepner said, and also readability. Inheriting from ABC makes it clear to your readers what you're up to.

Copy>>> from abc import ABC, abstractmethod
>>> 
>>> class Foo:
...     @abstractmethod
...     def f(self):
...         pass
... 
>>> class Bar(Foo):
...     pass
... 
>>> Bar().f()
>>> 
>>> class Baz(ABC):
...     @abstractmethod
...     def f(self):
...         pass
... 
>>> class Quux(Baz):
...     pass
... 
>>> Quux().f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Quux with abstract methods f
๐ŸŒ
CSDN
devpress.csdn.net โ€บ python โ€บ 63045e257e6682346619a9dd.html
Python ABC Multiple Inheritance_python_Mangs-Python
from my_abc import MyABC from third_party_package import SomeClass class MyClass(MyABC, SomeClass): def __init__(self): super(MyClass, self).__init__() def print(self): print('Hello ABC') ... TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases ยท I understand that I can create a class the inherits directly from my interface MyABC, and then create another class which then inherits from both this class which I created and my third party module class.
๐ŸŒ
Real Python
realpython.com โ€บ inheritance-composition-python
Inheritance and Composition: A Python OOP Guide โ€“ Real Python
January 11, 2025 - The Employee class in the example above is what is called an abstract base class. Abstract base classes exist to be inherited, but never instantiated. Python provides the abc module to formally define abstract base classes.