Python Learning notes Call the Eval function Invalid syntax error problem

Source: Internet
Author: User
The use and distribution of Pytho is completely free, it is an object-oriented language, it's. Its class module supports high-level concepts such as polymorphism, operator overloading, and multiple inheritance, and is very easy to use with Python's simple syntax and type. Python includes a number of pre-compiled and portable functional modules called the standard library. Python can invoke C and C + + libraries, can be called by C and C + + programs, can be integrated with Java components, communicate with frameworks such as COM and. NET, and can interact with the network through interfaces such as soap, Xml-rpc, and CORBA, so Python is more than just a standalone tool.

Originally intended to use the Eval function to assign a value to the variable, did not expect the invalid syntax error occurred. The source code is as follows

In [2]: eval (' a = 1 ') of File "
 
  
   
  ", line 1 a = 1  ^syntaxerror:invalid
 Syntax
  

Baidu did not Baidu to the results, and finally found a good answer on the StackOverflow.

The author means that the Eval function is only responsible for processing the expression, and there is no function to assign a value, that is, the Eval function is only responsible for outputting your input, true or false, or something. However, it does not have the ability to affect the current code environment. The EXEC () function should be used if we want to use the assignment. Look at the code:

In [3]: exec (' a = 1 ') in [4]: aout[4]: 1

The solution to the problem has been given, so let's look at the official documentation for the two functions.

eval (expression, Global=none, Local=none)

The parameters are string and optional global and local. Global should be a dictionary file, and local should be a mapping object.

The expression parameter will be processed as a python (strictly speaking, a list of conditional statements), and the global and local parameters will be used as both globally and locally as namespaces.

EXEC (Object[,global,[locals])

This function can provide Python with dynamic code execution capabilities.

  • 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.