I have a few functions (hardcoded, I don't want Python to compute the derivatives, in reality the functions are more complex):
def f(x): return x
def g(x): return x ** 2
def f_prime(x): return 1
def g_prime(x): return 2 * x
and a list a functions:
myfunctions = [f, f, g, f]
How to get the list of the related "prime" functions?
I was thinking about:
myderivatives = [globals()[function.__name__ + '_prime'] for function in myfunctions]
and it works:
[<function f_prime at 0x00000000022CF048>, <function f_prime at 0x00000000022CF048>, <function g_prime at 0x00000000022CF0B8>, <function f_prime at 0x00000000022CF048>]
but is there a more Pythonic way to get the list of the "prime" functions associated to the original versions?
Also there is a corner case:
from module1 import thisfunction as f
def f_prime(x):
pass
globals()[f.__name__ + '_prime']
# doesn't work because f.__name__ + '_prime' is 'thisfunction_prime' and not 'f_prime'