内置 isidentifier() 函数结果不一致

Built-in isidentifier() function inconsistent results

我正在使用内置的 isidentifier() 函数来查找允许用于变量名的 Unicode 字符(我知道 xid_start 和 xid_continue 字符,不需要解释)。以下程序与它在不同系统上的结果存在一定的不一致。我很困惑,对推理很感兴趣。

chars = []

for char in range(0x110000):
    char = chr(char)
    if char.isidentifier() or ('a' + char).isidentifier():
        chars += [char]

print(len(chars))

PyCharm 中的程序结果 运行 给我 134415,但是 repl.it gives me 128770. My python version is 3.9.7, while repl's is 3.8.12. Everything I was able to find was this isidentifier() documentation, which gives a hint at PEP 3131 standard which is used in Python 3. But both I and repl.it 中的 运行 它使用相同的主要 python 版本,它只是次要版本区别。寻找功能变更日志也没有结果。希望您能帮我解决这个问题!

他们使用不同版本的 unicode 数据

尝试添加到您的脚本中

import unicodedata

print(unicodedata.unidata_version)

对我来说,repl.it 使用的是版本 12.1.0,而我在 mac 12.3 上的 python 3.9.9 使用的是版本 13.0.0

你 link 说这些字符取决于 DerivedCoreProperties.txt 文件,该文件是 python

使用的 unicode 版本

Version 12.1.0 Version 13.0.0

The exact specification of what characters have the XID_Start or XID_Continue properties can be found in the DerivedCoreProperties file of the Unicode data in use by Python


这与 unicodedata 模块在其 docs 中所说的一致。

使用时python 3.8

The data contained in this database is compiled from the UCD version 12.1.0.

使用时python 3.9

The data contained in this database is compiled from the UCD version 13.0.0.