Python 函数的 kwargs 参数的键是否保证为字符串类型?
Are the keys of a kwargs argument to Python function guaranteed to be type string?
def func(**kwargs):
for key, value in kwargs.items():
# Is key always going to be a string?
# Could it have spaces?
pass
Python.
中关于 kwargs 的两个问题
- 是否保证kwargs的每个键都是str类型?如果是这样,我可以避免类型检查。
- 如果#1 为真,是否保证每个键都没有空格?
kwargs
中的keywords
要遵循变量名规则,full_name
是有效的变量名(也是有效的keyword
),full name
不是有效的变量名(也不是有效的 keyword
)。
来自 PEP 362 -- Function Signature Object:
A Parameter object has the following public attributes and methods:
name : str
- The name of the parameter as a string. Must be a valid python identifier name (with the exception of POSITIONAL_ONLY
parameters, which can have it set to None
.)
并且,来自 Docs:
2.3. Identifiers and keywords:
...
Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters A
through Z
, the underscore _
and, except for the first character, the digits 0
through 9
.
...
直接传递的关键字参数必须是有效的 Python 标识符,是的,它将始终被视为字符串。其他任何东西都是 SyntaxError
.
f(foo=1) # Works
f($=1) # Fails
f(1=1) # Fails
不过,你也可以通过解包的方式给出关键字参数。在这种情况下,您的关键字参数必须仍然是字符串,但它们可以采用任何格式。
让我们定义一个虚拟函数来测试它。
def f(**kwargs):
print(kwargs)
关键字参数可以包含 space 或者是一串数字。它甚至可以包含特殊字符。
f(**{"hello world": 'foo'}) # prints {'hello world': 'foo'}
f(**{"1": 'foo'}) # prints {'1': 'foo'}
f(**{"$": 'foo'}) # prints {'$': 'foo'}
关键字参数必须是字符串。其他任何东西都是 TypeError
.
f(**{1: 'foo'}) # TypeError: f() keywords must be strings
f(**{b'foo': 1}) # TypeError: f() keywords must be strings
其他人已经涵盖了这方面的实际方面,但我想知道文档是否真的对关键字词典的类型做出了任何保证。
documentation on call semantics 没有具体说明关键字参数在字典中的类型为 str
。
这就是文档中关于传递给使用 **
声明的参数的关键字参数值类型的全部内容:
a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary
因此,从理论上讲,文档似乎允许您接收关键字参数作为 bytes
,但这在 Python 3.
的任何当前实现中都不会发生
(对于 Python 2,同样成立,但对于 unicode
而不是 bytes
。)
def func(**kwargs):
for key, value in kwargs.items():
# Is key always going to be a string?
# Could it have spaces?
pass
Python.
中关于 kwargs 的两个问题- 是否保证kwargs的每个键都是str类型?如果是这样,我可以避免类型检查。
- 如果#1 为真,是否保证每个键都没有空格?
kwargs
中的keywords
要遵循变量名规则,full_name
是有效的变量名(也是有效的keyword
),full name
不是有效的变量名(也不是有效的 keyword
)。
来自 PEP 362 -- Function Signature Object:
A Parameter object has the following public attributes and methods:
name : str
- The name of the parameter as a string. Must be a valid python identifier name (with the exception ofPOSITIONAL_ONLY
parameters, which can have it set toNone
.)
并且,来自 Docs:
2.3. Identifiers and keywords:
... Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase lettersA
throughZ
, the underscore_
and, except for the first character, the digits0
through9
. ...
直接传递的关键字参数必须是有效的 Python 标识符,是的,它将始终被视为字符串。其他任何东西都是 SyntaxError
.
f(foo=1) # Works
f($=1) # Fails
f(1=1) # Fails
不过,你也可以通过解包的方式给出关键字参数。在这种情况下,您的关键字参数必须仍然是字符串,但它们可以采用任何格式。
让我们定义一个虚拟函数来测试它。
def f(**kwargs):
print(kwargs)
关键字参数可以包含 space 或者是一串数字。它甚至可以包含特殊字符。
f(**{"hello world": 'foo'}) # prints {'hello world': 'foo'}
f(**{"1": 'foo'}) # prints {'1': 'foo'}
f(**{"$": 'foo'}) # prints {'$': 'foo'}
关键字参数必须是字符串。其他任何东西都是 TypeError
.
f(**{1: 'foo'}) # TypeError: f() keywords must be strings
f(**{b'foo': 1}) # TypeError: f() keywords must be strings
其他人已经涵盖了这方面的实际方面,但我想知道文档是否真的对关键字词典的类型做出了任何保证。
documentation on call semantics 没有具体说明关键字参数在字典中的类型为 str
。
这就是文档中关于传递给使用 **
声明的参数的关键字参数值类型的全部内容:
a dictionary containing the excess keyword arguments (using the keywords as keys and the argument values as corresponding values), or a (new) empty dictionary
因此,从理论上讲,文档似乎允许您接收关键字参数作为 bytes
,但这在 Python 3.
(对于 Python 2,同样成立,但对于 unicode
而不是 bytes
。)