当使用单星号或双星号打开字典时,有人能解释其中的区别吗?当在函数参数中使用时,你可以提到它们的差异,只有当它在这里相关时,我不这么认为
然而,可能存在一些相关性,因为它们共享相同的星号语法。
def foo(a,b)
return a+b
tmp = {1:2,3:4}
foo(*tmp) #you get 4
foo(**tmp) #typeError: keyword should be string. Why it bothers to check the type of keyword?
此外,为什么字典的键在THIS情况下作为函数参数传递时不允许是非字符串的?有什么例外吗?为什么他们这样设计Python,是因为编译器无法推导出这里的类型还是其他什么?
当字典作为列表进行迭代时,迭代会获取它的密钥,例如
for key in tmp:
print(key)
与相同
for key in tmp.keys():
print(key)
在这种情况下,拆包为*tmp
等效于*tmp.keys()
,忽略这些值。如果要使用这些值,可以使用*tmp.values()
。
双星号用于定义具有关键字参数(如(的函数
def foo(a, b):
或
def foo(**kwargs):
在这里,您可以将参数存储在字典中,并将其作为**tmp
传递。在第一种情况下,键必须是带有函数公司中定义的参数名称的字符串。在第二种情况下,您可以将kwargs
作为函数中的字典来使用。
def foo(a,b)
return a+b
tmp = {1:2,3:4}
foo(*tmp) #you get 4
foo(**tmp)
在这种情况下:foo(*tmp)
表示foo(1, 3)
foo(**tmp)
表示foo(1=2, 3=4)
,这将引发错误,因为1
不能是参数。Arg必须是字符串,并且(感谢@Alexander Reynolds指出这一点(必须以下划线或字母字符开头。参数必须是有效的Python标识符。这意味着你甚至不能做这样的事情:
def foo(1=2, 3=4):
<your code>
或
def foo('1'=2, '3'=4):
<your code>
有关更多详细信息,请参阅python_basic_syntax。
这是一个扩展的可迭代解包。
>>> def add(a=0, b=0):
... return a + b
...
>>> d = {'a': 2, 'b': 3}
>>> add(**d)#corresponding to add(a=2,b=3)
5
对于单个*,
def add(a=0, b=0):
... return a + b
...
>>> d = {'a': 2, 'b': 3}
>>> add(*d)#corresponding to add(a='a',b='b')
ab
点击此处了解更多信息。
我认为函数参数和开箱字典中的**双星号直观地表示:
#suppose you have this function
def foo(a,**b):
print(a)
for x in b:
print(x,"...",b[x])
#suppose you call this function in the following form
foo(whatever,m=1,n=2)
#the m=1 syntax actually means assign parameter by name, like foo(a = whatever, m = 1, n = 2)
#so you can also do foo(whatever,**{"m":1,"n":2})
#the reason for this syntax is you actually do
**b is m=1,n=2 #something like pattern matching mechanism
so b is {"m":1,"n":2}, note "m" and "n" are now in string form
#the function is actually this:
def foo(a,**b): # b = {"m":1,"n":2}
print(a)
for x in b: #for x in b.keys(), thanks to @vlizana answer
print(x,"...",b[x])
现在所有的语法都有意义了。单个星号也是如此。值得注意的是,若使用单个星号来解包dictionary,实际上是在尝试以列表方式解包,并且只有dictionary的键被解包。
[https://docs.python.org/3/reference/expressions.html#calls]
这样做的结果是,尽管*表达式语法可能出现在显式关键字参数之后,但它是在关键字参数(以及任何**表达式参数——请参阅下文(之前处理的。因此:
def f(a, b):
print(a, b)
f(b=1, *(2,))
f(a=1, *(2,))
#Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
#TypeError: f() got multiple values for keyword argument 'a'
f(1, *(2,))