What does Eval do?

The string expression is parsed and executed, returning a value

Syntax format

eval(expression[, globals[, locals]]) 
Copy the code

Expression: expression string

Globals: It must be a dictionary

Locals: Can be any map object

The simplest expression chestnut

A chestnut

Print (eval (" 123 ")) print (eval (" True ")) print (eval () "(1, 2, 3)") print (eval () "[1, 2, 3]") # 123 True output (1, 2, 3) [1, 2, 3]Copy the code

Chestnut 2

Print (eval("1+2")) x = 1 print(eval('x+1')Copy the code

Chestnuts three

Print (eval("[a,b]"))Copy the code

Take globals

G = {"x": 5} print(eval("x+1", gCopy the code

The globals parameter is provided in eval

The scope of eval is the dictionary specified by G. The outer x = 10 is masked. Eval is invisible, so x = 5 is used. Finally, if your time is not very tight, and want to quickly improve, the most important thing is not afraid of hardship, I suggest you can price @762459510, that is really very good, many people progress quickly, need you not afraid of hardship oh! You can go to add a look at ~

x = 10 y = 5 g = {"x": Print (eval("x+1+y", g)) File "<string>", line 1, in <module> NameError: name 'y' is not definedCopy the code

Bonus: Private Reply [01] get a free introduction to Python tutorial video

An error is reported because the global argument has no y value

Take the locals

# use locals a = 1 g = {" a ": 2," b ": 3} l = {30," b ":" c ": 4} print (eval (" a + b + c, g, l)) # 36 output resultsCopy the code
  • Eval’s scope becomes globals + locals
  • The locals scope has a higher priority than globals
  • The values in the locals parameter override the values in the globals parameter

String to dictionary

Jsons = "{'a':123,'b':True}" print(type(eval(jsons))Copy the code

Take globals

Print (eval (" {' name ':' Linux ', 'age: the age} ", {" age ": 123})) # output {' name' : 'Linux', 'age: 123}Copy the code

Take the locals

Print (eval (" {' name ':' Linux ', 'age: the age} ", "age" : 123}, {} "age" : 24)) # output {' name' : 'Linux', 'age: 24}Copy the code

Built-in function chestnut

# built-in function print (eval (" dir () ")) print (eval (" abs (10))) # output [' __annotations__ ', '__builtins__', '__cached__', '__doc__, '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'g', 'jsons', 'l', 'x', 'y'] 10Copy the code

Chestnuts reporting errors

A chestnut

Print (eval(" AA ")) print(eval(" AA ")) File "<string>", line 1, in <module> NameError: name 'aa' is not definedCopy the code

Chestnut 2

Print (eval("[a,b,c]") print(eval("[a,b,c]")) File "<string>", line 1, in <module> NameError: name 'c' is not definedCopy the code

Bonus: Private Reply [01] get a free introduction to Python tutorial video

Chestnuts three

Print (eval("if x: print(x)")) File <string>", line 1 if x: print(x) ^ SyntaxError: invalid syntaxCopy the code

Because eval() only accepts expressions any other statements (such as if, for, while, import, def, class) will raise an error:

This is an example of how to use an eval function in Python. For more information about the eval function in Python, see other articles.