[Tutor] best way to dynamically set class variables?

Peter Otten __peter__ at web.de
Thu Nov 8 02:46:22 EST 2018


Alan Gauld via Tutor wrote:

>>exec() might even be a way
> 
> It's a way but it's a bad way! :-)

And then

> A simple approach you could use would be to get Python to
> generate a new python file(module) containing the required class
> definition (simple string processing) and then dynamically
> import the new file.

That's basically exec(), with better tracebacks and a higher chance to run 
outdated code ;)

By the way I don't think exec() is bad as long as you control its input and 
as long as this input is fairly simple.




More information about the Tutor mailing list