'Accessing the base classes of a class in a class decorator
I want to disable multiple inheritance for a class, so, it could only inherit from one class. One way to do this is through a metaclass, but I want to do it using a class decorator.
class NoMultipleInheritance(type):
def __new__(cls, name, bases, clsdict):
if (len(bases)>1):
raise Exception('multiple inheritance not allowed')
return super().__new__(cls, name, bases, clsdict)
class B(metaclass=NoMultipleInheritance):
pass
class C:
pass
class A(B, C):
pass
gives
Exception: multiple inheritance not allowed
But how do I get the base classes in a class decorator?
def no_multiple_inheritance(cls):
# how do I get the base classes of a class here?
return cls
class B:
pass
class C:
pass
@no_multiple_inheritance
class A(B, C):
pass
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
