exec

Dynamic/runtime method creation (code generation) in Python

Dynamic/runtime method creation (code generation) in Python Question: I need to generate code for a method at runtime. It’s important to be able to run arbitrary code and have a docstring. I came up with a solution combining exec and setattr, here’s a dummy example: class Viking(object): def __init__(self): code = ”’ def dynamo(self, arg): …

Total answers: 6