Python 3.8 supports using a limited set of non-ASCII Unicode characters in identifiers. So, it seems that it is valid to use as a character in an identifier.
However, something is wrong...
Problem
def f():
print(f'{=}')
f(1)
f(=2)
f(**{'': 3})
Run Code Online (Sandbox Code Playgroud)
The first two calls are fine, but the third fails:
=1
=2
Traceback (most recent call last):
File "sigma.py", line 24, in <module>
f(**{'': 3})
TypeError: f() got an unexpected keyword argument ''
Run Code Online (Sandbox Code Playgroud)
Analysis
Let's see what is actually going on:
def f2(**kw):
for name, value in kw.items():
print(f'{name}={value} {ord(name)=}')
f2(=2)
f2(**{'': 3})
Run Code Online (Sandbox Code Playgroud)
It prints:
?=2 ord(name)=931
=3 ord(name)=120506
Run Code Online (Sandbox Code Playgroud)
I called it with both times, but it was changed to the very similar simpler ? in the first call.
It seems that an argument named (U+1D6BA) is implicitly renamed to ? (U+03A3), and in every call to the function, argument is also implicitly renamed to ?, except if it is passed as **kwargs
.
The Questions
Is this a bug? It does not look like it is accidental. Is it documented? Is there a set of true characters and a list of alias characters available somewhere?
我认为这是由于 Python 处理字符的方式造成的。
\n如果您使用提供的西格玛字母之一设置变量: \xce\xa3 或 ,您也可以使用另一个来访问它。知道这两个片段都有效:
>>> \xce\xa3 = 5\n>>> \n5\n
Run Code Online (Sandbox Code Playgroud)\n>>> = 5\n>>> \xce\xa3\n5\n
Run Code Online (Sandbox Code Playgroud)\n您可以看到globals()
它被分配给 \xce\xa3 (ord: 931)
\n我的猜测是 Python 在执行变量查找之前修改了该字符。
\n类似的讨论,由我在 github/wtfpython 中发布
归档时间: |
|
查看次数: |
83 次 |
最近记录: |