python-3.xvariadic-functionsarity

Python splat-in-the-middle


You can put splat arguments in the middle rather than at the end (only in python 3 it seems):

import functools
def wierd_sum(use_str_cat=False, *args, use_product=False):
    if use_str_cat:
        return ''.join([str(a) for a in args])
    elif use_product:
        return functools.reduce(lambda a,b : a*b, args)
    else:
        return functools.reduce(lambda a,b : a+b, args) 

Now how do you use said function?

print(wierd_sum(1, 2, 3)) # 23  -> Concatenation, 1 counts as True.
print(wierd_sum(False, 2, 3, 4, 5, True)) # 15 -> Addition, True counts as 1.
print(wierd_sum(False, 2, 3, 4, 5, use_product=True)) # 120 -> Multiplication 2*3*4*5
print(wierd_sum(use_str_cat=True, 1, 2, 3)) # SyntaxError: positional argument follows keyword argument.
print(wierd_sum(1, 2, 3, use_str_cat=False)) # TypeError: wierd_sum() got multiple values for argument 'use_str_cat'

My question is, is there ever, ever, EVER a reason to do such a thing?


Solution

  • This is to allow for keyword only arguments; similar to positional-only arguments (comparison here).

    Ideally though, unless you actually need the varargs, you should omit the name to disallow varargs from being supplied:

    def my_func(a, *, keyword_only=True):
       pass
    
    my_func(1, 2)  # TypeError: my_func() takes 1 positional argument but 2 were given
    
    my_func(1, keyword_only=2)  # Fine