Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python: How to dynamically call a method/function given a string of its name
# Based on reading :
class Foo(object):
def dynamic_call(self, attribute_name):
method_name = 'calculate_' + attribute_name # e.g. given attribute name "baz" call method "calculate_baz"
func = getattr(self, method_name) # find method that located within the class
func() # execute the method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment