问题描述
我已经宣布了字典
data=dict(key="sadasfd",secret="1213",to="23232112",text="sucess",from='nattu')
它在python中显示错误,表示使用了关键字.为什么不使用from
?
It is showing error in python, saying that keyword is used. Why does it is not taking from
?
每当我遇到from
作为字典中的键时,我就无法使用它.
Whenever I encounter with from
as a key in dictionary, I can't use it.
推荐答案
from
是保留的关键字,并且不能用作dict()
构造函数的关键字参数.
from
is a reserved keyword and cannot be used as a keyword argument to the dict()
constructor.
使用 {...}
字典文字:
Use a {...}
dictionary literal instead:
data = {'key': "sadasfd", 'secret': "1213",
'to': "23232112", 'text': "sucess", 'from': 'nattu'}
或随后分配给密钥:
data['from'] = 'nattu'
或避免完全使用保留关键字.
or avoid using reserved keywords altogether.
Python支持将任意关键字传递给可调用对象,并使用字典来捕获此类参数,因此dict()
构造函数接受关键字参数是逻辑上的扩展.但是此类参数 限于有效的 Python标识符 仅.如果您想使用其他任何内容(保留关键字,以整数开头或包含空格的字符串,整数,浮点数,元组等),请改用Python dict文字语法.
Python supports passing arbitrary keywords to a callable, and uses dictionaries to capture such arguments, so it is a logical extension that the dict()
constructor accepts keyword arguments. But such arguments are limited to valid Python identifiers only. If you want to use anything else (reserved keywords, strings starting with integers or containing spaces, integers, floats, tuples, etc.), stick to the Python dict literal syntax instead.
这篇关于python中的字典声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!