Python built-in function--eval

Source: Internet
Author: User

Python built-in function--eval

Eval

Evaleval (expression [, globals [, locals]])

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 some modules can be used.
If this parameter defaults, the current global namespace for which the function is currently invoked is used; The
parameter locals is a local scope namespace, which is used to specify the local namespace that is 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, and
means that the built-in module can be used regardless of the setting.
If you use the default method for two namespaces, you will use the namespace that you call this function to find the appropriate variable. Why does the
Use this function? The reason for this function is the difference between a dynamic language and a compiled language,
because it is basically impossible to generate code dynamically in a compiled language,
but dynamic language is possible, meaning the software has been deployed to the server,
but with minimal changes, Having to modify this part of the code directly, you can immediately implement the changes without reloading 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 do destructive action, is actually a virus.
>>> eval (' + + ')
2

#全局命名空间为空, using local namespaces >>> 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)

3

#使用全局命名空间 >>> 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, all Owed, {}) >>> F = make_fng (' cos (x) ') >>> print F (9) -0.911130261885>>> f = make_fng (' cos (x*x) ') >>> print F (9) 0.776685982022

The above is the python built-in function--eval content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

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