简体   繁体   English

尝试添加到字典时,Python输出类型“ Nonetype”

[英]Python outputs type 'Nonetype' when try to add to dictionary

Here is the code(Running Python 2.7.6): 这是代码(运行Python 2.7.6):

currency_pairs = {'PPC': 10}
print currency_pairs
currency_pairs = currency_pairs.update({'NMC': 50})
print type (currency_pairs)

Output: 输出:

{'PPC': 10}
<type 'NoneType'>

Why won't Python add to the dictionary? 为什么Python不会添加到字典中? I don't understand this. 我不明白 Any thoughts? 有什么想法吗?

The method update just updates the dictionary but don't return anything (in other words, returns None ) In the line 该方法update仅更新字典,但不返回任何内容(换句话说,返回None

currency_pairs = currency_pairs.update({'NMC': 50})

you are assigning None to currecy_pairs . 您将None分配给currecy_pairs The method itself, will modify the dictionary, so you should call it like this: 该方法本身将修改字典,因此您应该这样调用它:

currency_pairs = {'PPC': 10}
print currency_pairs
currency_pairs.update({'NMC': 50})
print currency_pairs

Output: 输出:

{'PPC': 10}
{'PPC': 10, 'NMC': 50}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM