I am trying to create a Python class with a class attribute the receive a dictionary with all subclasses object.
Below I make a example code within what I am trying to accomplish, but I can't make the classmethod run on class initialization. I have the feeling that I am approaching the problem in the wrong way, so any suggestions will be very welcome.
class Client:
enable = True
subclass_dict = {}
def __init__(self, category, limit):
self.category = category
self.limit = limit
if Client.enable == True:
Client.enable = False
Client.get_subclasses()
@classmethod
def get_subclasses(cls):
subclasses = cls.__subclasses__()
for subclass in subclasses:
cls.subclass_dict[subclass().category] = subclass()
def __str__(self):
return f'{self.category}'
def __repr__(self):
return f'Client: {self.category}'
class SpecialClient(Client):
category = 'Special'
limit = '10_000'
def __init__(self):
super().__init__(SpecialClient.category, SpecialClient.limit)
class NormalClient(Client):
category = 'Normal'
limit = 1_000
def __init__(self):
super().__init__(NormalClient.category, NormalClient.limit)