0

""" I have three python functions:a(), b(), c(), which have almost the same process flow.The difference place is just 'x', 'y', 'z', the three are just a part of a function name or a variable name. How can I write these functions friendly and elegantly? """

def a():
    do_x()
    some = var1['x_id']
    var2['x_result'] = x_process()
    ...

def b():
    do_y()
    some = var1['y_id']
    var2['y_result'] = y_process()
    ...
def c():
    do_z()
    some = var1['z_id']
    var2['z_result'] = z_process()
    ...
funing
  • 1
  • 1
  • Take a look at [this][1] post. It might help [1]: http://stackoverflow.com/questions/7719466/how-to-convert-a-string-to-a-function-in-python – ltiritilli Nov 27 '14 at 02:38

3 Answers3

3

These three functions are basically identical, except for which functions they call, and which map indices they use. So you can just pass the called functions and map indices as arguments to a unified function:

def do_something(do_fun, id1, id2, process_fun):
    do_fun()
    some = var1[id1]
    var2[id2] = process_fun()

do_something(do_x, 'x_id', 'x_result', x_process)
user3426575
  • 1,723
  • 12
  • 18
2

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").

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Thank you!I got your advice.These codes are writen by other people, and be used in many places, but i will try to restructure it. – funing Nov 27 '14 at 03:01
0

You can use lambda functions:

def a():
    print 'a'

def b():
    print 'b'

def c():
    print 'c'

def a_process():
    print 'a'

def b_process():
    print 'b'

def c_process():
    print 'c'


def func(x):
    do = {'a':lambda: a(), 'b':lambda: b(), 'c':lambda:c()}
    do[x]()
    some = var1['{}_id'.format(x)]
    process = {'a':lambda: a_process(), 'b':lambda: b_process(), 'c':lambda:c_process()}
    var2['{}_result'.format(x)] = process[x]()


func('c')
shley
  • 203
  • 1
  • 6