[python] the use and explanation of the eval() function

Learning eval() well can make us get twice the result with half the effort when using python, but it also has some disadvantages (discussed in the previous article "How to convert a string into a dictionary")

For example:

1. Convert the string lst_str="[1,2,3,4]" into a list and use eval(lst_str), the result is the list type [1,2,3,4]

2. The same string dict_str="{"name":"fandy","age":"18"}" is converted into a dictionary type {"name":"fandy","age":"18" using eval(dict_str) "}

Here we can look at the expression of eval():

eval(expression[, globals[, locals]]) 

illustrate:

  • expression: expression , type is string
  • globals: global variables , must be a dictionary
  • locals: local variables , which can be any mapping object
  • The expression data types must be consistent , otherwise an error will be reported

TypeError: can only concatenate str (not "int") to str

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Correct way of writing:

a=1
g={"a":"aaa"}
l={"b":"bbb"}
res=eval("a+b+’hello'", g,l)# 结果为aaabbbhello
a=1
l={"a":50,"b":20}
g={"a":10}
res=eval("a+b+1", g,l)#结果为71

 Wrong way of writing:

TypeError: can only concatenate str (not "int") to str

a=1
g={"a":"aaa"}
l={"b":"bbb"}
res=eval("a+b+2", g,l)# 运行报错

 TypeError: unsupported operand type(s) for +: 'int' and 'str'

a=1
l={"a":50,"b":20}
g={"a":10}
res=eval("a+b+’hello'", g,l)#运行报错

 It can be seen that eval() can remove the quotation marks in the expression and convert it into the corresponding type

eval(“[1,2]”)---->list[1,2]

eval(“{"a":1}”)---->字典{"a":1}

eval("1+2")---->int type 1+2

1. When there are only expression expressions

a=1
res=eval("a+3")#结果是4

 It can be seen that when there is only an expression, eval() directly executes the expression inside, and the output result is equivalent to print(1+3)

2. When the expression expression and the global variable globals exist at the same time

a=1
g={"a":100}
res=eval("a+3", g)#好结果为103

When the expression expression and the global variable globals  exist at the same time , taking the data of the global variable globals will mask out a=1. In the above example, the value of a is a=100 in the global variable, and the output result is equivalent to print(100+3)

It is said above that it must be a dictionary, otherwise an error will be reported:

TypeError: globals must be a real dict; try eval(expr, {}, mapping)

3. When there are expression expressions and global variables globals and local variables locals at the same time

a=1
g={"a":100}
l={"a":50,"b":20}
res=eval("a+b+3", g,l)#结果为73

When the expression expression and the global variable globals and the local variable locals exist at the same time , if the global variable and the local variable have the same key, the value of the corresponding key takes the value in the local variable locals;

In the above example, both the global variable g and the local variable l have a, so a in the expression a+b+3 takes a=50 in the local variable l, b only exists in the local variable, so b=20, output The result is equivalent to print(50+20+3)

Guess you like

Origin blog.csdn.net/weixin_43569834/article/details/131175972