python 如何解析unicode变量,在Python中定义Unicode变量

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 = 'abc∂´ƒ©'

ƒ = 'My name is'

ß = '˚ß˙ˆ†ˆ∆ ßå®åø©ˆ'

print('s =', s)

print('ƒ =', ƒ, 'ß =', ß)

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.

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:

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值