[Python]python Reflection (cont.)-exec__python

Source: Internet
Author: User

As an interpretative scripting language, there is essentially no reflection at all. Because any string can be interpreted as code.

There are eval in Perl, which is also in Python, but is subdivided into eval and exec.

This can be regarded as the ultimate reflex killing device, due to the power is too great, python3 in the grammar of some restrictions.

Allows exec to import into local variable space instead of global space.

A step closer than the loading of the module, we can put a portion of the code, or the rules, templates, stored in a remote centralized server or configuration center, and then dynamically load through eval. This eliminates the hassle of distributing code to different machines.

In extreme cases, the executing server can only run a common framework, and core code is all fetched and injected from the central server. This is a bit like Erlang's gensvr idea.

        Str= '
def dev_r_ci_iox_cpu (): "" "
    dev_r_ci_iox_cpu" "
    oids=". 1.3.6.1.2.1.1.1.0 "
    ret=_ Httpsnmpget (OIDs) return
    ret
t=dev_r_ci_iox_cpu
' '
    #str =rpc.getfile (FID)   
    _locals = locals ()  
    exec (Str,globals (), _locals)  #用局部变量来注入
    f=_locals[' t ']      #模板中的函数名只有核对意义.



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.