Answer the question
In order to leave comments, you need to log in
How to make indefinite inheritance in python?
There are N-number of classes:
class Validator:
class ApparelBeauty(Validator):
class ElectronicsSport(Validator):
class AutomaticWorker(active_class = check_active_category()):
def check_active_category():
...
if config.getboolean('category', 'Apparel&Beauty') == True:
return ApparelBeauty.__name__
Answer the question
In order to leave comments, you need to log in
In general, the very need for such code is already a sign of serious problems.
But if you really need to ... In python, classes are objects of the first category. You can do everything with them as with other objects, for example, return them from functions. So there is no need to mess around with class_name.
There is such a solution through metaclasses :
import inspect
my_dog_age = 7
class SmallDog:
pass
class MiddleDog:
pass
class BigDog:
pass
class DogMeta(type):
def __new__(mcs, name, bases, namespace):
dog = SmallDog
if my_dog_age >= 10:
dog = BigDog
elif my_dog_age >= 5:
dog = MiddleDog
return super().__new__(mcs, name, (dog,), namespace)
class Dog(metaclass=DogMeta):
pass
class MyDog(Dog):
pass
print(inspect.getmro(MyDog))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question