It's not about ASCII vs. non-ASCII (your "non-ASCII" is still ASCII, it's just punctuation, not alphanumeric). CPython, as an implementation detail, interns string constants that contain only "name characters". "Name characters" in this case means the same thing as the regex escape w
: Alphanumeric, plus underscore.
Note: This can change at any time, and should never be relied on, it's just an optimization they happen to use.
At a guess, this choice was made to optimize code that uses getattr
and setattr
, dict
s keyed by a handful of string literals, etc., where interning means that the dictionary lookups involved often ends up doing pointer comparisons and avoiding comparing the strings at all (when two strings are both interned, they are definitionally either the same object, or not equal, so you can avoid reading their data entirely).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…