星操作员是什么意思?是什么*运算符在Python中的意思,例如在类似的代码中zip(*x)或f(**k)?它是如何在解释器内部处理的?它会影响性能吗?是快还是慢?什么时候有用,什么时候没用?应该在函数声明或调用中使用它吗?
3 回答
开心每一天1111
TA贡献1836条经验 获得超13个赞
*
def sum(a, b): return a + b values = (1, 2)s = sum(*values)
s = sum(1, 2)
**
values = { 'a': 1, 'b': 2 }s = sum(**values)
def sum(a, b, c, d): return a + b + c + d values1 = (1, 2)values2 = { 'c': 10, 'd': 15 }s = sum(*values1, **values2)
s = sum(1, 2, c=10, d=15)
*x
**y
def sum(*values): s = 0 for v in values: s = s + v return s s = sum(1, 2, 3, 4, 5)
**
:
def get_a(**values): return values['a']s = get_a(a=1, b=2) # returns 1
def sum(*values, **options): s = 0 for i in values: s = s + i if "neg" in options: if options["neg"]: s = -s return s s = sum(1, 2, 3, 4, 5) # returns 15s = sum(1, 2, 3, 4, 5, neg=True) # returns -15s = sum(1, 2, 3, 4, 5, neg=False) # returns 15
添加回答
举报
0/150
提交
取消