3

I am following this tutorial:

http://www.pyimagesearch.com/2015/04/20/sorting-contours-using-python-and-opencv/#comment-405768

and in one of the lines there is the function:

(cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes),
        key=lambda b:b[1][i], reverse=reverse))

I want to know what is the use of the asterisk before the sorted function call.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • https://docs.python.org/3/tutorial/controlflow.html#tut-unpacking-arguments – Andrew Li Sep 08 '16 at 03:38
  • 1
    This question has helped because the explanation provided by Vic Yu is very short and efficient. We also need sometime quick answers for particular situations. There are similar questions posted here but they are more general and have received very good and in depth answers but in some time-pressing situations one might need a quick and short explanation. – DorinPopescu Nov 16 '17 at 08:34

2 Answers2

9

Asterisk is unpacking operator:

>>> list(range(3, 6))            # normal call with separate arguments
[3, 4, 5]
>>> args = [3, 6]
>>> list(range(*args))            # call with arguments unpacked from a list
[3, 4, 5]

More about unpacking operator:

https://docs.python.org/3/tutorial/controlflow.html#unpacking-argument-lists

Vic Yu
  • 118
  • 1
  • 7
0

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments; if there are positional arguments x1, ..., xN, and expression evaluates to a sequence y1, ..., yM, this is equivalent to a call with M+N positional arguments x1, ..., xN, y1, ..., yM.

MotKohn
  • 3,485
  • 1
  • 24
  • 41