Defining unicode variables in Python

后端 未结 2 889
刺人心
刺人心 2021-01-20 20:58

Recently, I have been reading about the Python source code encoding, especially PEP 263 and PEP 3120.

I have the following code:

# coding:utf-8

s =          


        
2条回答
  •  死守一世寂寞
    2021-01-20 21:26

    No, Python 2 only supports ASCII names. From the language reference:

    identifier ::=  (letter|”_”) (letter | digit | “_”)*
    letter     ::=  lowercase | uppercase
    lowercase  ::=  “a”…”z”
    uppercase  ::=  “A”…”Z”
    digit      ::=  “0”…”9”
    

    Compared that the much longer Python 3 version, which does have full Unicode names.

    The practical problem the PEPs solve is that before, if a byte over 127 appeared in a source file (say inside a unicode string), then Python had no way of knowing which character was meant by that as it could have been any encoding. Now it's interpreted as UTF-8 by default, and can be changed by adding such a header.

提交回复
热议问题