我知道功能map
。它被用于类似
new_list = map(func, list)
但是是什么*map(func, list)
意思呢?它被用作
hands = set(best_hand(h) for h in itertools.product( *map(replacements, hand)))
Run Code Online (Sandbox Code Playgroud)
这意味着从返回的可迭代对象map()
将被解压为函数的参数。也就是说,不是调用函数并将可迭代对象作为单个参数传递,而是将可迭代对象的各个元素作为单独的参数传递。
这种技术的一个例子:
>>> 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
Run Code Online (Sandbox Code Playgroud)
但是foo()
在不解包的情况下调用x
意味着您传递了一个需要三个参数的参数:
>>> foo(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() takes exactly 3 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
如果语法
*expression
出现在函数调用中,则expression
必须评估为可迭代的。来自这个可迭代对象的元素被视为额外的位置参数......
归档时间: |
|
查看次数: |
2039 次 |
最近记录: |