Last active
October 13, 2016 18:47
-
-
Save kdheepak/b6d8efac6cbdc6090999a3cb1d03c855 to your computer and use it in GitHub Desktop.
This file contains 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 kwargs2args(f): | |
argspec = inspect.getargspec(f) | |
no_arg_only = len(argspec.defaults) - len(argspec.args) | |
defaults = { argspec.args[no_arg_only+i]: d for i, d in enumerate(argspec.defaults)} | |
def function(*args, **kwargs): | |
old_args = list(argspec.args) | |
new_args = list() | |
for arg in argspec.args: | |
if arg not in kwargs: | |
new_args.append(defaults[old_args.pop(0)]) | |
new_args = new_args + list(args) | |
f(*new_args) | |
function.argspec = argspec | |
return function | |
@kwargs2args | |
def foo(x=1, y=1, *args): | |
print('x={}'.format(x), 'y={}'.format(y), args) | |
foo(1, 2) | |
# ('x=1', 'y=1', (1, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment