Home Calling a function of a module by using its name (a string)
Reply: 0

Calling a function of a module by using its name (a string)

user4938
1#
user4938 Published in September 21, 2018, 8:05 am

What is the best way to go about calling a function given a string with the function's name in a Python program. For example, let's say that I have a module foo, and I have a string whose contents are "bar". What is the best way to go about calling foo.bar()?

I need to get the return value of the function, which is why I don't just use eval. I figured out how to do it by using eval to define a temp function that returns the result of that function call, but I'm hoping that there is a more elegant way to do this.

share|improve this question

10 Answers 10

active oldest votes
up vote 1486 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.340692 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO