It is not uncommon to want to define many different functions which shall have the same prototype in C.
int f(int x, int y, char z, char *w);
int g(int x, int y, char z, char *w);
int h(int x, int y, char z, char *w);
int i(int x, int y, char z, char *w);
int j(int x, int y, char z, char *w);
To leave open the possibility of adding an additional parameter to every single one of these functions without having to change many lines of code, I can use the preprocessor to stay flexible:
#define FUNCTION(func) int (func)(int x, int y, char z, char *w)
and then replace my prototypes with
FUNCTION(f);
FUNCTION(g);
FUNCTION(h);
FUNCTION(i);
FUNCTION(j);
And then when I go to define the functions I would use lines like:
FUNCTION(f)
{
//do something with x,y,z,w
}
FUNCTION(g)
{
//do something else with x,y,z,w
}
Is there a way to accomplish this in python? i.e. is it possible define many functions in Python, all of which take exactly the same parameters, then modify their parameters (say adding or removing one) by changing a single line?
You don't forward-declare functions in Python the way you might in C, so the premise of this question doesn't make a lot of sense. The closest analogy would be in type declarations, which you can indeed alias:
from typing import Callable
FUNCTION = Callable[[int, int, str, str], int]
You can also have functions that themselves return functions, and potentially eliminate a lot of repetition that way, but your example doesn't include the body of the functions so that doesn't really map.
For the case you describe in your edit, you could just not declare the argument types and use *args instead:
def f(*args):
x, y, z, w = args
Or for a typesafe solution (since it's hard to type *args and **kwargs), you could have these functions take a single (typed) tuple argument:
FUNCTION_ARGS = Tuple[int, int, str, str]
def f(args: FUNCTION_ARGS):
# next line will throw a mypy error if it doesn't match FUNCTION_ARGS
x, y, z, w = args