Python standard library: built-in function eval (expression, globals = None, locals = None)

Source: Internet
Author: User

Python standard library: built-in function eval (expression, globals = None, locals = None)

This function is a string used to dynamically execute an expression or a code object compiled by the compile function. The expression parameter is an expression string or the name of the compiled code object. The globals parameter is a global namespace and can be used to specify the scope of the global scope during expression execution, for example, you can specify certain modules. If this parameter defaults, the current global namespace of the currently called function is used; the locals parameter is a local-scope namespace, which is used to specify the local namespace accessed when the expression is executed. If the global namespace parameter appears, but the default built-in module is used, the module is automatically copied to the global namespace, which means that the built-in module can be used no matter how configured. If both namespaces use the default method, the namespaces used to call this function are used to find the corresponding variables.

Why is this function used? The reason for this function should be the difference between the dynamic language and the compilation language, because it is basically impossible to dynamically generate code in the compilation language, but the dynamic language is acceptable, this means that the software has been deployed on the server, but as long as few changes are made, you have to directly modify this part of the code to implement changes immediately without reloading the entire software. In addition, this function can be used in machine learning. For example, you can dynamically modify the code based on the frequency and method of using the software to adapt to user changes. Think of it here, is it vital to be able to update code and make improvements? If destructive actions are done, they are actually a virus.

Example:

# Eval () print (eval ('1 + 1') # The global namespace is empty. Use the local namespace def make_fn (code): import math ALLOWED_LOCALS = {v: getattr (math, v) for v in filter (lambda x: x. startswith ('_'), dir (math)} return eval ('lambda x: % s' % code, None, ALLOWED_LOCALS) f = make_fn ('x + 1 ') print (f (2) # use the global namespace def make_fng (code): import math ALLOWED = {v: getattr (math, v) for v in filter (lambda x: not x. startswith ('_'), dir (math)} ALLOWED ['_ builtins _'] = None return eval ('lambda x: % s' % code, ALLOWED, {}) f = make_fng ('cos (x) ') print (f (9) f = make_fng ('cos (x * x )') print (f (9 ))

The output result is as follows:

2

3

-0.9111302618846769

0.7766859820216312


Cai junsheng QQ: 9073204 Shenzhen

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.