I have module foo, inside this module I dynamically created class:
def superClassCreator():
return type("Bar", (object,), {})
Now, what I want to achieve is to make this new dynamic class visible as a class of this module:
import foo
dir(foo)
>>> [... 'Bar' ...]
Do you know how to do this?
You can use
Bar = superClassCreator()infoo(at the module level).Alternatively, from another module, you can add
Baras an attribute onfoo:or, if the name must be taken from the generated class:
From within the
foomodule, you can set it directly onglobals():with an optional
delstatement to remove thegeneratedClassname from the namespace again.