I know function map. It is used something like
new_list = map(func, list)
But what does *map(func, list) mean? It was used like
hands = set(best_hand(h) for h in itertools.product( *map(replacements, hand)))
I know function map. It is used something like
new_list = map(func, list)
But what does *map(func, list) mean? It was used like
hands = set(best_hand(h) for h in itertools.product( *map(replacements, hand)))
It means that the iterable returned from map() will be unpacked as arguments to the function. That is, rather than calling the function and passing the iterable object as a single parameter, the individual elements of the iterable are passed as individual parameters.
An illustration of this technique:
>>> def foo(a, b, c): print "a:%s b:%s c:%s" % (a, b, c)
...
>>> x = [1,2,3]
>>> foo(*x)
a:1 b:2 c:3
But calling foo() without unpacking x means you are passing one argument where three are expected:
>>> foo(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 3 arguments (1 given)
From the Python 3 reference ยง6.3.4:
If the syntax
*expressionappears in the function call,expressionmust evaluate to an iterable. Elements from this iterable are treated as if they were additional positional arguments ...