The reflection of Python

Source: Internet
Author: User

Importing modules as strings

In the form of a string, go to the module to look for the established function, and execute getattr (module name, function name, default value)

In the form of a string, the SetAttr (module name, function name/variable name, lambda expression/variable value) is set to the module.

Remove something delattr (module name, function name) from the module in the form of a string

To manipulate members of an object (a module) in the form of a string

 1  INP = input ( "  Please enter the module name:   " )  2  inp_func = input (  "  Enter the function you want to execute:   " )  3  dd = __ import__  (INP) #   import modules as strings 
# from XXX.XX Import oo is equivalent to __import__ (' xxx.xx.oo ', fromlist = True)
4 # looking for a function in the module as a string if no returned none is found
5 r = Target_func ()
6 Print (r)
# in-Memory operations
Hasattr (module name, function name) # to determine if a function exists
SetAttr (module name, function name/variable name, lambda expression/variable value)
Delattr (module name, function name)

Results:

Please enter the module name: Commons
Please enter the function to execute: F1
F1

The reflection of Python

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.