Skip to content

Instantly share code, notes, and snippets.

@alexboche
Created March 5, 2019 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexboche/c5d12ff690360a8af97dec569cdaf5d4 to your computer and use it in GitHub Desktop.
Save alexboche/c5d12ff690360a8af97dec569cdaf5d4 to your computer and use it in GitHub Desktop.
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