Extension of Python's eval -
this question has answer here:
string = "if x > 3 :\n print(\"x greater\")\nelse :\n print(\"x lesser\")" """ prints string indented code of if else statements if x greater 3 x greater else prints x lesser""" x = 6 eval(string) can eval() in python used in way or there i'm missing .
eval used expressions. documentation
the expression argument parsed , evaluated python expression
use exec here
>>> string = "if x > 3 :\n print(\"x greater\")\nelse :\n print(\"x lesser\")" >>> x = 6 >>> exec(string) x greater however note both statements quite risky use. (see eval dangerous)
Comments
Post a Comment