Have a method with the following signature:
def foo(self, bar, *uks):
return other_method(..., uks)
Normally this is called as:
instance.foo(1234, a, b, c, d)
However in some cases I need to do something like this:
p = [a, b, c, d]
instance.foo(1234, p)
At the receiving end this does not work, because other_method sees *args being made up of a single list object instead of simply a [a, b, c, d] list construct. If I type the method as:
def foo(self, bar, uks = []):
return other_method(..., uks)
It works, but then I’m forced to do this every time:
instance.foo(1234, [a, b, c, d])
It’s not a huge deal I guess, but I just want to know if I’m missing some more pythonic way of doing this?
Thanks!
Python supports unpacking of argument lists to handle exactly this situation. The two following calls are equivalent:
Regular call:
Argument list expansion: