- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我偶然发现了以下奇怪的情况:
>>> class Test:
µ = 'foo'
>>> Test.µ
'foo'
>>> getattr(Test, 'µ')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
getattr(Test, 'µ')
AttributeError: type object 'Test' has no attribute 'µ'
>>> 'µ'.encode(), dir(Test)[-1].encode()
(b'\xc2\xb5', b'\xce\xbc')
我输入的字符始终是键盘上的 µ 符号,但由于某种原因它被转换了。为什么会这样?
最佳答案
这里涉及两个不同的角色。一个是 MICRO SIGN ,这是键盘上的一个,另一个是GREEK SMALL LETTER MU .
要了解发生了什么,我们应该看一下 Python 如何在 language reference 中定义标识符:
identifier ::= xid_start xid_continue*
id_start ::= <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
id_continue ::= <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
xid_start ::= <all characters in id_start whose NFKC normalization is in "id_start xid_continue*">
xid_continue ::= <all characters in id_continue whose NFKC normalization is in "id_continue*">
我们的字符 MICRO SIGN 和 GREEK SMALL LETTER MU 都是 Ll
unicode 组(小写字母)的一部分,因此它们都可以在标识符中的任何位置使用。现在请注意,identifier
的定义实际上是指 xid_start
和 xid_continue
,它们被定义为相应非 x 定义中的所有字符,其NFKC 规范化为标识符生成有效的字符序列。
Python 显然只关心标识符的规范化 形式。这一点在下面得到证实:
All identifiers are converted into the normal form NFKC while parsing; comparison of identifiers is based on NFKC.
NFKC 是一个 Unicode normalization将字符分解成单独的部分。 MICRO SIGN 分解为希腊小写字母 MU,这正是其中发生的事情。
还有很多其他字符也受此规范化影响。另一个例子是 OHM SIGN分解成GREEK CAPITAL LETTER OMEGA .将其用作标识符会得到类似的结果,此处使用本地显示:
>>> Ω = 'bar'
>>> locals()['Ω']
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
locals()['Ω']
KeyError: 'Ω'
>>> [k for k, v in locals().items() if v == 'bar'][0].encode()
b'\xce\xa9'
>>> 'Ω'.encode()
b'\xe2\x84\xa6'
所以最后,这只是 Python 所做的事情。不幸的是,并没有真正好的方法来检测这种行为,从而导致如图所示的错误。通常,当标识符仅被称为标识符时,即它像真正的变量或属性一样使用时,那么一切都会好起来的:每次都运行规范化,并找到标识符。
唯一的问题是基于字符串的访问。字符串只是字符串,当然没有规范化发生(那只是个坏主意)。这里显示的两种方式,getattr
和 locals
, 两者都对字典进行操作。 getattr()
通过对象的 __dict__
访问对象的属性,而 locals()
返回一个字典。在字典中,键可以是任何字符串,因此在其中包含 MICRO SIGN 或 OHM SIGN 是完全没问题的。
在这些情况下,您需要记住自己执行规范化。我们可以利用 unicodedata.normalize
为此,这也允许我们从 locals()
内部正确获取我们的值(或使用 getattr
):
>>> normalized_ohm = unicodedata.normalize('NFKC', 'Ω')
>>> locals()[normalized_ohm]
'bar'
关于python - 标识符规范化 : Why is the micro sign converted into the Greek letter mu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50126274/
我是一名优秀的程序员,十分优秀!