Created
May 25, 2011 02:14
-
-
Save adreyer/990192 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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