I have a factory class with a method that generates classes dynamically.
This class is within a factory.py file.
class Factory:
def __init__(self, ast):
self.ast = ast
self.classes = {}
def run(self):
for item in self.ast:
self.classes[item.name] = type(item.classname, (), item.attributes)
Quite simple. The problem is that the generated classes are prefixed with factory. e.g.
<class 'factory.MyClass'> or <class 'factory.AnotherClass'>.
I would like to be able to change the scope in which these classes are defined, for instance to something like that :
<class 'CustomClasses.MyClass'>
But it seems that the prefix depends on the module in which the type() function is called. Is there any way to change this behavior ?
typefunction returns a problem? If you want to check thetype()of an instance ofFactory, then afterfrom factory import Factoryandf = Factory(),type(f) == FactoryisTrue. If this isn't your problem, then what is it?factory. That's an artifact of howtype.__repr__is defined. (Even if it did, the name of the class typically doesn't matter except for how__repr__renders it. It's not related to any variables referring to the class.)CustomClassesmodule and make them accessible this way rather than storing them in the local dictionnary.setattr(CustomClass, item.name) = type(item.classname, (), item.attributes)