public
Last active

Python reflection magic

  • Download Gist
inspectfun.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import inspect
 
def call(fun, **myArgs):
names, _, _, values = inspect.getargspec(builder)
defaults = zip(names, values)
valuesToCallWith = dict(defaults + myArgs.items())
return fun(**valuesToCallWith)
 
def builder(a = 0, b = 2, c = 3):
return a + b + c
 
print call(builder, a = 3) # prints 8
print call(builder, b = 9, c = 1) # prints 10
print call(builder, c = 11) # prints 13

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.