等于更新的字典的变量不起作用,为什么?
variable equal to an updated dict does not work, why?
假设我有
dict1 = {'uno':[1,2,3],'dos':[4,5,6]}
和
dictA = {'AA':'ZZZZZ'}
这个有效:
dict1.update(dictA)
结果:{'uno': [1, 2, 3], 'dos': [4, 5, 6], 'AA':'ZZZZZ'}
但这不起作用:
B = dict1.update(dictA)
结果不是错误,但结果是 None,这使得此行为 (IMMO) 奇怪且危险,因为代码不会崩溃。
那么为什么返回 None 而没有给出错误?
注意:
看起来要走的路是:
C = dict1.update(dictA)
B = {}
B.update(dict1)
B.update(dictA)
B
C 是 none
B在这里可以
当使用 update
时,它更新 dict1
作为参数给出的字典和 returns None
:
Docs:
dict. update([mapping])
mapping
Required. Either another dictionary object or an iterable of key:value pairs > (iterables of length two). If keyword arguments are specified, the dictionary is > then updated with those key:value pairs.
Return Value
None
代码:
dict1 = {'uno':[1,2,3],'dos':[4,5,6]}
dict1.update({'tres':[7,8,9]})
# {'uno': [1, 2, 3], 'dos': [4, 5, 6], 'tres': [7, 8, 9]}
print(dict1)
假设我有
dict1 = {'uno':[1,2,3],'dos':[4,5,6]}
和
dictA = {'AA':'ZZZZZ'}
这个有效:
dict1.update(dictA)
结果:{'uno': [1, 2, 3], 'dos': [4, 5, 6], 'AA':'ZZZZZ'}
但这不起作用:
B = dict1.update(dictA)
结果不是错误,但结果是 None,这使得此行为 (IMMO) 奇怪且危险,因为代码不会崩溃。
那么为什么返回 None 而没有给出错误?
注意: 看起来要走的路是:
C = dict1.update(dictA)
B = {}
B.update(dict1)
B.update(dictA)
B
C 是 none B在这里可以
当使用 update
时,它更新 dict1
作为参数给出的字典和 returns None
:
Docs:
dict. update([mapping]) mapping Required. Either another dictionary object or an iterable of key:value pairs > (iterables of length two). If keyword arguments are specified, the dictionary is > then updated with those key:value pairs. Return Value None
代码:
dict1 = {'uno':[1,2,3],'dos':[4,5,6]}
dict1.update({'tres':[7,8,9]})
# {'uno': [1, 2, 3], 'dos': [4, 5, 6], 'tres': [7, 8, 9]}
print(dict1)