WordPress网站建设宣传图片

Python支持中文变量名吗?

释放双眼,带上耳机,听听看~!

Python已经支持中文变量名

Python3.x 已经支持全面 Unicode 编码,支持使用中文作为变量名。(支持是支持,实际使用不使用是另外一回事哈哈,有时候用中文会有一些莫名其妙的问题…)

例如:

博主简介 = "博主是个超级无敌大帅哥"
print(f"{博主简介}") # 博主是个超级无敌大帅哥

python文档2.3.标识符和关键字截图:

python文档2.3.标识符和关键字

我们来看下翻译后的内容:

2.3.标识符和关键字

以下词典定义描述了标识符(也称为名称)。

Python 标识符的语法基于 Unicode 标准附件 UAX-31,并详细阐述和更改如下所示:另请参阅PEP 3131了解更多详情。

在 ASCII 范围内 (U+0001.U=007F),标识符的有效字符与 Python 2.x 中的字符相同:上壳和小写字母通过、下划线以及除第一个字符外的数字通过。AZ_09

Python 3.0 引入来自 ASCII 范围之外的其他字符(参见PEP 3131)。对于这些字符,分类使用单码数据模块中包含的Unicode字符数据库版本。

标识符的长度是无限的。案例意义重大。

identifier   ::=  xid_start xid_continue*
id_start     ::=  <all characters in general categories Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the Other_ID_Start property>
id_continue  ::=  <all characters in id_start, plus characters in the categories Mn, Mc, Nd, Pc and others with the Other_ID_Continue property>
xid_start    ::=  <all characters in id_start whose NFKC normalization is in "id_start xid_continue*">
xid_continue ::=  <all characters in id_continue whose NFKC normalization is in "id_continue*">

上述 Unicode 类别代码代表:

  • Lu- 大写字母
  • Ll – 小写字母
  • Lt -标题箱字母
  • Lm – 修饰函数
  • Lo- 其他信件
  • Nl – 字母编号
  • Mn – 非速度标记
  • Mc- 间距组合标记
  • Nd – 小数
  • Pc – 连接器标点符号
  • Other_ID_Start – Proplist中的字符的明确列表.txt以支持向后兼容性
  • Other_ID_Continue – 同样

解析时,所有标识符都转换为正常形式 NFKC;标识符的比较基于 NFKC。

列出 Unicode 4.1 的所有有效标识符字符的非规范 HTML 文件可在https://www.unicode.org/Public/13.0.0/ucd/DerivedCoreProperties.txt找到

参考:

https://docs.python.org/3/reference/lexical_analysis.html#identifiers

PEP 3131 — Supporting Non-ASCII Identifiers | Python.org

给TA打赏
共{{data.count}}人
人已打赏
Python笔记

python requests请求之timeout参数

2021-10-5 0:14:01

Python笔记

python模块requests参数stream

2021-10-29 22:39:13

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索