exec and eval
exec and evak can perform a string type of code
evak only be used in code that you know exactly what you want to do is
eval has a return value, - for the outcome of a simple calculation
exec did not return. - suitable for simple process control
Exec ( ' Print (123) ' ) the eval ( ' Print (123) ' ) Print (the eval ( ' . 1 + 2 +. 3 +. 4 ' )) # return a value Print ( Exec ( ' . 1 + 2 +. 3 +. 4 ' )) # no return value
For example exec
#应用exec code = '''for i in range(8): print('*'*i)''' exec(code)
complie
String code is compiled, the object code that can be executed or be evaluated through the eval exec statement.
compile () Usage
'' 'the compile (parameters: ast and string or object' code file name: the file when reading the code from the inside, otherwise empty ', implementation: eval or Exec)' ''
code1 = '1+2+3' h = compile(code1,'','eval') print(eval(h))
Note that when an interactive, need to exec mode to single line.