Python function daily talk-eval function

Source: Internet
Author: User
Tags function definition



function definition:


eval (expression, Globals=none, Locals=none)


Evaluates the string str as a valid expression and returns the result of the calculation. The globals and locals parameters are optional, and if the globals parameter is provided, it must be a dictionary type, or it can be any map object if the locals parameter is provided.


The global namespace of Python is stored in a Dict object called Globals (), and the local namespace is stored in a Dict object called locals (). We can use print (locals ()) to view all variable names and variable values in the function body.

Python version compatible:


Python2.7


python3.x

Eval () main role:


1) in the compilation language to generate code dynamically, is basically impossible, but dynamic language is possible, meaning that the software has been deployed to the server, but as long as a few changes, have to directly modify this part of the code, you can immediately realize the change without the entire software reload.


2) in Machin learning based on the user's use of the software frequency, as well as the way, can dynamically modify the code to adapt to user changes.

English Explanation:


The arguments is a string and optional globals and locals. If provided, globals must be a dictionary. If provided, locals can is 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 the Globals dictionary is present and lacks ' __builtins__ ', the current globals was copied into globals before Expressi On is parsed. This means that expression normally have full access to the standard Builtins module and restricted environments is PROPAG Ated. If the locals dictionary is omitted it defaults to the Globals dictionary. If both dictionaries is omitted, the expression was executed in the environment where eval () is called. The return value is the result of the evaluated expression. Syntax errors is reported as exceptions. Example:



Example:

A=1

G={' a ': 20}

Eval ("A+1", g)


Results:



Example 2, test globals, locals

x = 1

y = 1

NUM1 = eval ("X+y")

Print (NUM1)

def g ():

x = 2

y = 2

num3 = eval ("X+y")

Print (NUM3)

num2 = eval ("X+y", Globals ())

#num2 = eval ("X+y", Globals (), locals ())

Print (NUM2)

G ()


The value of NUM1 is the value of 2;NUM3 is also well understood, is the value of 4;num2? Given the globals () parameter, you should first find the global x and Y values, which are all 1, so it is clear that the num2 value is also 2. If you comment out the sentence, do the following sentence? According to the 3rd point, the result is 4.



Security issues:


Because of the type of eval, it is likely to be exploited by hackers, causing security problems.


How to avoid security problems?


1, self-written check function;


2. Replace with Ast.literal_eval



Python function daily talk-eval function

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.