1

I find that I run into this often. I'll have a core function with a bunch of parameters that needs to run a helper function with the same parameters. Right now I feel like I'm being too explicit when calling the helper function so it feels long winded:

def masterFunc(a, b, c, arg1=1, arg2=2, arg3=3):
    utilFunc(a, b, c, arg1, arg2, arg3)

def utilFunc(a, b, c, arg1=1, arg2=2, arg3=3):
    print "do the thing"

masterFunc("a", "b", "c")

I suppose I can use **kwargs to make it more procedural but creating the dictionary still feels long winded:

def masterFunc(a, b, c, arg1=1, arg2=2, arg3=3):
    utilFunc(**{"a": a,
                "b": b,
                "c": c,
                "arg1": arg1,
                "arg2": arg2,
                "arg3": arg3})

def utilFunc(**kwargs):
    print "do the thing"

Is there a more elegant way to pass the same arguments to another function? I feel like I'm missing something.

I don't want to use *args or **kwargs for masterFunc as I find it makes it too obscure to know at a glance what parameters it requires.

Green Cell
  • 4,677
  • 2
  • 18
  • 49
  • your first code snippet does not seem bad at all imho.. – Ma0 Feb 18 '19 at 08:03
  • And also https://stackoverflow.com/questions/42471083/perfect-forwarding-in-python , https://stackoverflow.com/questions/42499656/pass-all-arguments-of-a-function-to-another-function , ... – jamesdlin Feb 18 '19 at 08:04
  • Thanks @jamesdlin, the marked question looks like it'll do nicely. The other two links are more for classes and don't apply here (not sub-classing anything). – Green Cell Feb 18 '19 at 08:10
  • 1
    The first code with explicitly listed arguments is the best in my opinion. The only thing that _might_ be better is changing the design completely, so that you don't have to pass all of the arguments. But we don't know what the functions are doing, so it is not clear whether that should out should not be done. – zvone Feb 18 '19 at 08:11
  • Check out https://stackoverflow.com/a/582206/8561957, especially the answer from @kmkaplan – fountainhead Feb 18 '19 at 08:13
  • @zvone The functions are generating a 3d skeleton, so my original functions actually have a lot more arguments. But I think you're right, maybe it's a design issue. – Green Cell Feb 18 '19 at 08:13
  • You might want to think about wrapping those two functions as methods in an utility class, maybe? – ChatterOne Feb 18 '19 at 08:13

0 Answers0