Skip to content

Instantly share code, notes, and snippets.

@illbebach
Created December 9, 2020 00:13
Show Gist options
  • Save illbebach/636bc777ebf7f1aa32a976e90ec8e0c1 to your computer and use it in GitHub Desktop.
Save illbebach/636bc777ebf7f1aa32a976e90ec8e0c1 to your computer and use it in GitHub Desktop.
call python functions indirectly
''' demonstrate calling functions using function references in an array '''
import inspect
def get_func_name() -> str:
''' return a string of the caller's function name '''
return inspect.currentframe().f_back.f_code.co_name
def func1(arg1, arg2):
print(get_func_name(), arg1, arg2)
def func2(arg1, arg2):
print(get_func_name(), arg1, arg2)
def func3(arg1, arg2):
print(get_func_name(), arg1, arg2)
def func4(arg1, arg2):
print(get_func_name(), arg1, arg2)
def func5(arg1, arg2):
print(get_func_name(), arg1, arg2)
FUNCTIONS = [
func1,
func2,
func3,
func4,
func5,
]
# main - call each function through the array
for i in range(5):
FUNCTIONS[i](i+1, "i+2")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment