Sunday 10 May 2020

Convert string to Python class object?

Warningeval() can be used to execute arbitrary Python code. You should never use eval() with untrusted strings. (See Security of Python's eval() on untrusted strings?)
This seems simplest.
>>> class Foo(object):
...     pass
... 
>>> eval("Foo")
<class '__main__.Foo'>

from : https://stackoverflow.com/questions/1176136/convert-string-to-python-class-object

No comments:

Post a Comment