Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 10
    why would you need this, couldn't the function just iterate over the supplied list without it being expanded? Commented May 27, 2010 at 14:27
  • 38
    Sure, but then you would have to call it: s = sum((1, 2, 3, 4, 5)) or s = sum([1, 2, 3, 4, 5]), the *values option makes the call look like it takes a number of arguments, but they're packed up into a collection for the function code. Commented May 27, 2010 at 14:29
  • 15
    Here's the real benefit: you can write functions that wouldn't otherwise be possible if you need to have a variable number of arguments. For example, C's printf function, which has 1+n arguments, is tricky to write as an exercise for any beginning programmer. In Python, a beginner can write def printf(string_template, *args) and move on.
    – IceArdor
    Commented Nov 20, 2013 at 7:38
  • 2
    What happens if you (accidentally perhaps :p) unpack a dictionary with only one * instead of two? It seems to do something, it seems like a tuple comes out, but it is not so obvious what it is. (edit: ok I think the answer is that it just unpacks the keys, the values are discarded)
    – Ben Farmer
    Commented Sep 15, 2017 at 7:47
  • 2
    The last example implies that * and ** are not only doing the unpacking but also packing! See this excellent page codingame.com/playgrounds/500/…
    – H.C.Chen
    Commented Feb 17, 2019 at 4:06