0

Is it possible to dynamically create classes in the __init__.py file.

I'll give an example. Let's say I have this hierarchy:

script.py
module\
 --> __init__.py

In the python file script.py, I want to know if I can, with this call:

from module import my_class

dynamically create the my_class definition in the __init__.py

Julien
  • 87
  • 9
  • 2
    Even if you *can*, doesn't mean you *should*.... – Jared Smith Mar 18 '20 at 21:51
  • Also, there's no real significance to `__init__.py` here. – user2357112 Mar 18 '20 at 21:54
  • look into type class creation. see https://stackoverflow.com/a/15247892 for example, and more specifically, this line: `newclass = type(name, (BaseClass,),{"__init__": __init__})`. do you however have **good** reason to create a class on the fly? It's a little bit like a metaclass question: more often than not, if you gotta ask, you don't need it. – JL Peyret Mar 18 '20 at 21:54

0 Answers0