Defining unicode variables in Python

那年仲夏 提交于 2019-12-01 22:20:13

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.

I don't think that those two articles are about encoding in the sense of your variable name being a Beta-symbol for example, but regarding the encoding in the variable value.

so if you change your code to this example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

a = 'abc?´ƒ©'
b = 'My name is'
c = '°ß?ˆ†ˆ? ßå®åø©ˆ'
print 'a =', a # by the way, the brackets are only used in python 3, so they are also being displayed when running the code in python 2.7
print 'b =', b, 'c =', c 

Hope that answers your question

Greetings Frame

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!