1

EDIT:
As some people have clearly misunderstood the question.. I am NOT EVEN CLOSELY asking how to make a switch/case in python. Read it please before you vote for closing it.

I have n different functions that are all highly related but take slightly different arguments.
As a metaphor, consider that they represent n different sorting algorithms. Now, I want one big parameterized function sort which is called from the outside and then internally calls the correct sorting algorithm / the correct function.
As an additional requirement, it must be possible to give all these subfunctions different input information (different number of arguments).

I came up with a solution but I feel like this is bad style.. can anyone with more experience give his/her thoughts to this solution?

def function_one(*args):
    arg1, arg2 = args[0:2]
    # do something here
    
def function_two(*args):
    arg3, arg4 = args[2:4]
    # do something here

# imagine that here are more functions

def function_n(*args):
    arg1, arg3, arg5 = args[0], args[2], args[4]
    # do something here

switcher = {
    'one': function_one,
    'two': function_two,
    # ... imagine that here are more mappings
    'n': function_n
}

def some_super_function(arg0, arg1=None, arg2=None, arg3=None, arg4=None, arg5=None, ..., argN=None):
    return switcher.get(arg0)(arg1, arg2, arg3, arg4, arg5, ..., argN)
Lavair
  • 888
  • 10
  • 21
  • 1
    To me, it would make more sense for the functions to accept keyword arguments than positional. Slicing out chunks of positional arguments feels fragile. – Carcigenicate Mar 24 '21 at 18:05
  • 1
    If these functions are all taking different subsets of arguments they should be separate functions. You might want to look into [`singledispatch`](https://docs.python.org/3/library/functools.html#functools.singledispatch) which lets you define a generic function and then add new implementations for different input _types_. – Alex Mar 24 '21 at 18:06
  • 1
    @buran I don't think that's the question, given they're already using the `switch` equivalent. – Carcigenicate Mar 24 '21 at 18:06
  • 1
    @Carcigenicate, it looks like they are not sure this is good style. But it's up to OP to tell or give more information. – buran Mar 24 '21 at 18:07
  • 1
    I would make also "super function" have the same signature - like the other functions. Also as @Carcigenicate suggested - maybe switch all functions to take keyword arguments, e.g. something like `def spam(**kwargs):` and then the super function `def eggs(arg, **kwargs):` – buran Mar 24 '21 at 18:18

1 Answers1

0

I did not see the requirement to select functions based on the passed arguments, so you can pass some parameter that allows you to select a function.

def function_one(*args):
    print(f'run function_one, args - {args}')
    # do something here


def function_two(*args):
    print(f'run function_two, args - {args}')
    # do something here


# imagine that here are more functions

def function_n(*args):
    print(f'run function_n, args - {args}')
    # do something here


switcher = {
    'one': function_one,
    'two': function_two,
    # ... imagine that here are more mappings
    'n': function_n
}


def some_super_function(func: str, *args):
    return switcher[func](args)

some_super_function('one', 1, 2, 3) # run function_one, args - ((1, 2, 3),)
some_super_function('two', '12', 'asdasda', 11) # run function_two, args - (('12', 'asdasda', 11),)
some_super_function('n', [1, 2, 3], {1: 2}, [123, 321]) # run function_n, args - (([1, 2, 3], {1: 2}, [123, 321]),)

asanisimov
  • 382
  • 2
  • 8
  • I am probably going with a similar approach as I don't expect any constructive answers anymore since the question got wrongly flagged as duplicate. – Lavair Mar 24 '21 at 20:20