Skip to content

Instantly share code, notes, and snippets.

Created August 26, 2012 13:10
What would you like to do?
Using getattr() to construct function objects from strings
def color_red():
return 'I am red'
def color_green():
return 'I am green'
def color_blue():
return 'I am blue'
# Demo of constructing function object from strings
from __future__ import print_function
# module where the functions are defined
import funcs
# entry point when executed
if __name__=='__main__':
colors = ['red','blue','green']
for color in colors:
funcname = 'color_{0:s}'.format(color)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment