在Python中定义Unicode变量 [英] Defining unicode variables in Python
问题描述
最近,我一直在阅读有关Python源代码编码的信息,尤其是 PEP 263 和 PEP 3120 。
Recently, I have been reading about the Python source code encoding, especially PEP 263 and PEP 3120.
我有以下代码:
# coding:utf-8
s = 'abc∂´ƒ©'
ƒ = 'My name is'
ß = '˚ß˙ˆ†ˆ∆ ßå®åø©ˆ'
print('s =', s)
print('ƒ =', ƒ, 'ß =', ß)
此代码在Python3上工作正常,但在Python2.7中导致 SyntaxError
。
我确实知道这可能与它无关
所以,我想知道是否有一种方法可以在Python2中支持Unicode变量名称。
This code works fine for Python3 but results in a SyntaxError
in Python2.7 .
I do understand that this probably might have nothing to do with source code encoding.
So, I would like to know if there is a way to support Unicode variable names in Python2.
总而言之,我也很难弄清楚PEP到底要解决什么实际问题,以及如何(以及在哪里)利用所提出的解决方案。我已经读过很少的讨论,但是它们并没有给出我问题的答案,而是对正确语法的解释:
In all, I am also having a hard time figuring out what pragmatic problem the PEPs exactly aim to solve and how(and where) do I take advantage of the proposed solutions. I have read few discussions on the same but they do not present an answer to my question rather an explanation of the correct syntax:
- Correct way to define Python source code encoding
- Working with utf-8 encoding in Python source
- Where does this come from: -*- coding: utf-8 -*-
- Is '# -*- coding: utf-8 -*-' also a comment in Python?
推荐答案
否,Python 2仅支持ASCII名称。来自语言参考:
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"
相比之下, Python 3版本,它确实具有完整的Unicode名称。
Compared that the much longer Python 3 version, which does have full Unicode names.
PEP解决的实际问题是,如果源文件中出现127个以上的字节(例如在unicode字符串中),则Python无法知道该字符是指哪个字符,因为它可能是任何编码。现在,默认情况下它被解释为UTF-8,并且可以通过添加此类标头进行更改。
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.
这篇关于在Python中定义Unicode变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!