Python standard library: Built-in function eval (expression, Globals=none, Locals=none)

Source: Internet
Author: User
Tags cos

This function is a string used to dynamically execute an expression, or a code object compiled by the compile function. The argument expression is an expression string, or the name of the compiled code object; parameter globals is a global namespace that specifies the scope of the global scope when an expression is executed , such as specifying that certain modules can be used. If this parameter is defaulted, the current global namespace of the current call to this function is used , and the parameter locals is the local scope namespace, which is used to specify the local namespace to be accessed when an expression is executed. If the global namespace parameter appears, but the default built-in module, the module is automatically copied to the global namespace, meaning that the built-in module can be used regardless of the setting. If two namespaces are used by default, the namespace used when the function is called to find the appropriate variable.

Why use this function? The reason for this function, should be dynamic language and compiled language differences, because 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, Do not reload the entire software. Another, this function can be used in machine learning, for example, according to the user to use the software frequency, as well as the way, can dynamically modify the code to adapt to user changes. Think of here, is not the ability to have vitality, can self-update code, to achieve improved progress, if destructive action, is actually a virus.

Example:

#eval () Print (eval (' 1 + 1 ') #全局命名空间为空, using 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, AL lowed_locals) F = make_fn (' x + 1 ') print (f (2)) #使用全局命名空间def Make_fng (code):   Import Math   allowed = {v:getattr (math , V) for the      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 results are as follows:

2

3

-0.9111302618846769

0.7766859820216312


Cai Junsheng qq:9073204 Shenzhen

Python standard library: Built-in function eval (expression, Globals=none, Locals=none)

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.