Created
March 5, 2019 11:11
-
-
Save alexboche/c5d12ff690360a8af97dec569cdaf5d4 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
Function only works with keyword arguments. Something like arkalii's solution is a good way of dealing with the issue. You could also use a decorator on the add function to map argument names. This would keep the mappings clean: | |
arg_map = { | |
"num1": "x", | |
"n": "x", | |
"num2": "y", | |
"m": "y", | |
} | |
def replace_args(func): | |
def new_func(*_, **kwargs): | |
# Generate a new kwargs dictionary with replaced keys. | |
new_kwargs = {} | |
for k, v in kwargs.items(): | |
if k in arg_map: | |
new_kwargs[arg_map[k]] = kwargs[k] | |
return func(**new_kwargs) | |
return new_func | |
@replace_args | |
def add(x, y): | |
return x + y | |
@replace_args | |
def minus(x, y): | |
return x - y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment