I have a Class Factory that generates classes with dynamic name assignment I like to be able to access them from other modules which import a package A currently I am loading the classes into a dictionary and updating the vars() of the init.py which to me seems not like a good idea, is there a better way of doing it?
class Model_Factory:
def __init__(self,list_of_names:list):
self._models={}
for name in list_of_names:
self.models[name]=Model_Factory.create_model(name)
@property
def models(self):
return self._models
@classmethod
def create_model(cls,cls_name):
def __init__(self, **kwargs):
super(type(self),self).__init__( **kwargs)
pass
return type(snake_case(cls_name), (parent),
{'__init__': __init__,
'aomething' : 'something',
'attribute_name':'value'})
then inside the package A's init.py
import whatever
some line of code
dic=Model_Factory().models
vars().update(dic)
Then when I want to access those classes all I have to do is to
from A import Dynamically_Created_Class_Name
This works perfectly fine and the other modules who need to import those classes know the dynamic names themselves alright, however this just does not seem to me the correct way of doing it, is there a method or built in method that would add class definition or a dictionary into the current module namespace?