I'm trying to make some classes in runtime with type()
and I want to set a Meta
class while creating them. Is it possible?
Asked
Active
Viewed 44 times
0

no746
- 408
- 6
- 23
-
Does this answer your question? [Is there a way to set metaclass after the class definition?](https://stackoverflow.com/questions/5120688/is-there-a-way-to-set-metaclass-after-the-class-definition) – Tomerikoo Feb 07 '21 at 13:32
1 Answers
0
I digged more and based on gotten errors, I came up with this solution, which seems working:
attr = dict() # Add methods, vars, etc. to this dict
meta = type("Meta", (type,), {"some_var": "some_value"})
attr["Meta"] = meta
print(attr)
cls = type("class_name", (object,), attr)

no746
- 408
- 6
- 23