In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use the eval () function of Python". In the daily operation, I believe that many people have doubts about how to use the eval () function of Python. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use the eval () function of Python". Next, please follow the editor to study!
Syntax and parameters
The syntax format of the evel () function in Python is eval (expression, globals=None, locals=None), followed by the globals parameter and the locals parameter. The eval () function executes a string expression and returns the value of that expression.
Expression: expression, as mentioned above, the evel function is used to execute a string expression, and the contents of the expression are placed here. When an expression refers to
Globals: this part must be a dictionary! It has to be a dictionary! It has to be a dictionary! Otherwise, the program will go wrong. When the globals parameter is defined, the scope of the eval function is limited to the globals.
Locals: this parameter controls the local namespace, function, and globals type, but when the parameter conflicts, the parameter at locals is executed.
2. Example of expression parameters (eval ("axi1"))
The running result is 11.
[parsing]: because globals and locals are not specified here, the contents of the expression section are executed directly. The effect of this program is equivalent to that of axi10 print (axi1).
3. Example of globals parameters: print 10 / 10 / g = {'astat1 / 4} print (eval ("axi1", g))
The running result is 5.
[parsing]: because globals is now specified, the scope of the expression part is within the dictionary specified by globals. So at this point, the outside axi10 is masked and takes the value in the dictionary.
4. Example of locals parameters: aq10bcm 20cm2 30g = {'afiuzhuo 6 magnificent caterpillar 8} t = {'bizzag 100 precinct caterpillar 10} print (eval (' ajar bauhinia clockwise))
The running result is 116
[analysis]: according to the exercises of the above topic, we know that when there are globals and locals, the scope of action is in globals and locals, so the axiom 10 and 20 will not be applied. The values of an and c go to the values in the dictionary g and t, respectively, and take the values in locals when both globals and locals have the parameter b. So axiom6, baking, 100, writing, 10.
Fifth, the danger of eval function
The eval function is so convenient that we can use one line of code to implement the calculator function print (eval ('please enter')). But because it has the ability to convert strings into expressions to execute, it can also execute system commands. This can easily be used by people with ulterior motives to execute system commands and delete key system files.
6. The official document of eval () function The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can be any mapping object. The expression argument is parsed and evaluated as a Python _ expression (technically speaking, a condition list) using the globals and locals dictionaries as global and local namespace. If theglobals dictionary is present and lacks'_ _ builtins__', the current globals are copied into globals before expression is parsed. This means that expression normally has full access to the standard builtins module and restricted environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both dictionaries are omitted, the expression is executed in the environment where eval () is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example: > x = 1 > > eval ('xylene 1') 2 This function can also be used to execute arbitrary code objects (such as those created by compile ()). In this case pass a code object instead of a string. If the code object has beencompiled with 'exec' as the mode argument, eval ()' s return value will be None. Hints: dynamic execution of statements is supported by the exec () function. The globals () and locals () functions returns the current global and local dictionary, respectively, which may be useful to pass around for use by eval () or exec (). See ast.literal_eval () for a function that can safely evaluate strings with expressions containing only literals. Common functions attached to eval () function include
1. Evaluate a valid expression in a string and return the result
> eval ('pow (2Power2)') 4 > eval ('2 + 2') 4 > eval ("n + 4") 85
2. Convert the string to the corresponding object (such as the conversion between list, tuple, dict and string)
> > a = "[1dje 2], [3je 4], [5pr 6], [7pr 8], [9je 0]] > > b = eval (a) > > b [[1jue 2], [3jue 4], [5je 6], [7je 8], [9jue 0]] > > a =" {1lburex fux scene 2R eval yy'} "> > c = jungyy (a) > > c {1dric fuxscene 2drodroyy'} > > a =" (1p2) 3) > d = eval (a) > d (1,2,3)
3. Reverse the string converted by backquotation marks back to the object
> list1 = [1List1`] > `list1`'> > type (`list1`) > type (eval (`list1`)) > a = eval (`list1`) > a] at this point, the study of "how to use the eval () function of Python" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.