Search code examples
pythonfunctionargskeyword-argument

Python 3.X using multiple *args for different stuff


I know that *args are used when you dunno how many args are being used inside the function call. However, what do I do when I want one group of *args to do X and one group of *args to do y?


Solution

  • You can not pass two *args within the single function. You need to pass args1 and args2 as plain list and you may pass these lists as args to the functions doing X and Y. For example:

    def do_X(*args):
        # Do something
    
    def do_Y(*args):
        # Do some more thing
    
    def my_function(list1, list2):
        do_X(*list1)  # Pass list as `*args` here
        do_Y(*list2)
    

    And your call to my_function would be like:

    args_1 = ['x1', 'x2']  # Group of arguments for doing `X`
    args_2 = ['y1', 'y2']  # Group of arguments for doing `Y`
    
    # Call your function
    my_function(args_1, args_2)