Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to call a function of a module from a string with the function's name in Python?

Let's say I have a module fibo, and it has a function whose name contains a string “baz”. How do I call this function?.


1 Answer
Manogna

The following code should call the required function

import fibo
result = getattr(fibo, 'baz')
print result

Similarly we can use getattr class instance bound methods, module-level methods, class methods and so on.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.