Evaluating Dynamically Generated Statements In Python
I need to dynamically generate python code and execute it with eval() function. What I would like to do is to generate some 'imports' and 'assign values'. I mean, I need to generat
Solution 1:
You want exec
instead of eval
.
>>> s = "x = 2"
>>> exec s
>>> x
2
Of course, please don't use exec
on untrusted strings ...
Solution 2:
This may also work:
x = """[
__import__("testContextSummary").TestContextSummary,
__import__("util.testGroupUtils").testGroupUtils.TestGroupUtils]
"""
testDB=eval(x)
Post a Comment for "Evaluating Dynamically Generated Statements In Python"