在Unicode标准中有(至少)3种希腊字母pi的变体,它们具有不同的代码点。但是,如果我将它们中的任何一个作为标识符分配给Python对象,我也可以使用其他任何一个:
In [1]: π = 3.14
In [2]: print(π) # \u03c0
3.14
In [3]: print() # \U0001d6d1
3.14
In [4]: print() # \U0001d70b
3.14
Python如何知道这些名称是相同的?
发布于 2018-06-17 01:01:36
作为原始的PEP3131 explained
所有标识符在解析时都转换为范式NFKC;标识符的比较基于NFKC。
文档的Identifiers and Keywords部分也介绍了这一点。
因此:
In [58]: pis = ["\u03c0", "\U0001d6d1", "\U0001d70b"]
In [59]: pis
Out[59]: ['π', '', '']
In [60]: [ord(pi) for pi in pis]
Out[60]: [960, 120529, 120587]
In [61]: import unicodedata
In [62]: [unicodedata.normalize('NFKC', pi) for pi in pis]
Out[62]: ['π', 'π', 'π']
In [63]: [ord(unicodedata.normalize('NFKC', pi)) for pi in pis]
Out[63]: [960, 960, 960]
https://stackoverflow.com/questions/50889843
复制相似问题