How to invoke a function from a string in Python

Source: Internet
Author: User
In Python, you can call a function based on a string:

1. Use GetAttr to invoke a function from a string

In a multi-process, it is possible to pass over a string, then how can I invoke an already existing function, mainly to use the function of the GetAttr function, the function is to use a string to get the string corresponding to the function of the object, and then it is possible to execute, as follows:

In the module, there are two functions:

[Root@python 530]# cat attr.py#!/usr/bin/env pythondef Kel ():  print ' This is a Kel function ' Def smile ():  print ' th IS is a smile function ' if __name__ = = ' __main__ ':  Kel ()  Smile ()

In the attr module above, two functions are defined, one function is Kel, the other function is smile, then I execute the function according to the string Kel and smile, that is, using the GetAttr function, as follows:

>>> Import attr>>> k = getattr (attr, ' Kel ') >>> K () This is a kel function>>> s = getattr (attr, ' smile ') >>> s () This was a smile function>>> e = getattr (attr, ' Errors ') Traceback (most recent call L AST): File "
 
  
   
  ", line 1, in 
  
   
    
   attributeerror: ' Module ' object have no attribute ' errors '
  
    c7/>
  

In the first, the module is imported directly, then the module is an object, so that the getattr based on the string Kel to get to the Kel () function in attr, so that the final execution, here is implemented, depending on the string, thus executing the corresponding function.

2. Use a dictionary to invoke a function

The definition of the above module does not change, but when called, you can define a dictionary that executes the function according to the value of the dictionary, as follows:

>>> Import attr>>> d = {' Kel ': Attr.kel, ' smile ':attr.smile}>>> d[' Kel '] () This is a Kel function>>> d[' smile '] () This is a smile function

You can then use the dictionary value to invoke the function.

The above two methods are mainly used, when passing over a string, I how to call other functions, then the first method is to use GetAttr to execute the function, the second method is to pre-define a dictionary, and then execute the dictionary value.

Above this python in accordance with the string call function implementation method is small to share all the content of everyone, hope to give you a reference, but also hope that we support the script home.

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