Skip to content

Instantly share code, notes, and snippets.

@adreyer
Created May 25, 2011 02:14
Show Gist options
  • Select an option

  • Save adreyer/990192 to your computer and use it in GitHub Desktop.

Select an option

Save adreyer/990192 to your computer and use it in GitHub Desktop.
import inspect
def binder(fun, *args, **kwargs):
""" given a function fun, args and kwargs will bind the values or
defaults and return a dictionary. Doesn't do anything with
variable args or kwargs now """
arg_spec = inspect.getargspec(fun)
arg_names = arg_spec[0]
defaults = arg_spec[3]
binding = {}
# how many unused defaults are there
unused_defs= len(args) - (len(arg_names) - len(defaults))
# bind them all here
for arg, arg_name in zip(args+defaults[unused_defs:],arg_names):
binding[arg_names] = arg
# now overwrite the kwargs that were supplied
for key, val in kwargs.items():
binding[key] = val
return binding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment