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

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -