The best way is not just to rewrite those functions, but to restructure the various items they make use of, by changing them from variables with x, y, or z in the names to items stored in a structure that maps the strings "x", "y", and "z" to the appropriate stuff. Something like this:
do_funcs = {'x': do_x, 'y': do_y, 'z': do_z}
# make ids whatever is in var1
# but with the keys as "x", "y", "z" instead of "x_id", "y_id", "z_id"
ids = {}
# make resu;ts whatever is in var2
# but with the keys as "x", "y", "z" instead of "x_result", "y_result", "z_result"
results = {}
processes = {'x': x_process, 'y': y_process, 'z': z_process}
Then you can write one function:
def do_stuff(which):
do_funcs[which]()
some = ids[which]
results[which] = processes[which]()
And then you call do_stuff('x')
, do_stuff('y')
, or do_stuff('z')
.
That is just a sketch, because exactly how to do it best depends on where those other things are defined and how they're used. The basic, idea, though, is to not use parts of variable names as parameters. If you find yourself with a bunch of things called x_blah
, y_blah
and z_blah
, or even dict keys like "x_id"
and "y_id"
, you should try to restructure things so that they are data structures directly parameterized by a single value (e.g., a string "x"
, "y"
or "z"
).