python类名称中的有效字符 [英] Valid characters in a python class name

查看:116
本文介绍了python类名称中的有效字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态创建python类,并且我知道并非所有字符在此上下文中都是有效的.

I'm dynamically creating python classes, and I know not all characters are valid in this context.

类库中是否有方法可以用来清理随机文本字符串,以便可以将其用作类名?要么该列表,要么一个允许的字符列表将是一个很好的帮助.

Is there a method somewhere in the class library that I can use to sanitize a random text string, so that I can use it as a class name? Either that or a list of the allowed characters would be a good help.

与标识符名称冲突的补充:就像@Ignacio在下面的答案中指出的那样,任何保留字作为类名.但是有一个陷阱.如果您确实使用保留字,则将无法像其他(非动态创建的)类一样使该类可访问(例如,通过执行globals()[my_class.__name__] = my_class).在这种情况下,保留字始终优先.

Addition regarding clashes with identifier names: Like @Ignacio pointed out in the answer below, any character that is valid as an identifier is a valid character in a class name. And you can even use a reserved word as a class name without any trouble. But there's a catch. If you do use a reserved word, you won't be able to make the class accessible like other (non-dynamically-created) classes (e.g., by doing globals()[my_class.__name__] = my_class). The reserved word will always take precedence in such case.

推荐答案

Python 3

Python语言参考,§2.3,标识符和关键字"

Python中标识符的语法基于Unicode标准附件UAX-31,其详细说明和更改如下所述;有关更多详细信息,另请参见PEP 3131.

The syntax of identifiers in Python is based on the Unicode standard annex UAX-31, with elaboration and changes as defined below; see also PEP 3131 for further details.

在ASCII范围(U + 0001..U + 007F)中,标识符的有效字符与Python 2.x中的相同:大写和小写字母A至Z,下划线_和,除了第一个字符,数字0到9.

Within the ASCII range (U+0001..U+007F), the valid characters for identifiers are the same as in Python 2.x: the uppercase and lowercase letters A through Z, the underscore _ and, except for the first character, the digits 0 through 9.

Python 3.0引入了ASCII范围之外的其他字符(请参阅PEP 3131).对于这些字符,分类使用unicodedata模块中包含的Unicode字符数据库版本.

Python 3.0 introduces additional characters from outside the ASCII range (see PEP 3131). For these characters, the classification uses the version of the Unicode Character Database as included in the unicodedata module.

标识符的长度是无限的.情况很重要.

Identifiers are unlimited in length. Case is significant.

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类别代码代表:

The Unicode category codes mentioned above stand for:

  • Lu-大写字母
  • Ll-小写字母
  • Lt-标题大写字母
  • Lm-修饰语字母
  • Lo-其他字母
  • Nl-字母数字
  • Mn-非间距标记
  • MC-间隔组合标记
  • Nd-十进制数字
  • PC-连接器标点符号
  • Other_ID_Start-PropList.txt中的显式字符列表,以支持向后兼容
  • Other_ID_Continue-同样
  • Lu - uppercase letters
  • Ll - lowercase letters
  • Lt - titlecase letters
  • Lm - modifier letters
  • Lo - other letters
  • Nl - letter numbers
  • Mn - nonspacing marks
  • Mc - spacing combining marks
  • Nd - decimal number
  • Pc - connector punctuations
  • Other_ID_Start - explicit list of characters in PropList.txt to support backwards compatibility
  • Other_ID_Continue - likewise

所有标识符在解析时都转换为普通形式的NFKC;标识符的比较是基于NFKC.

All identifiers are converted into the normal form NFKC while parsing; comparison of identifiers is based on NFKC.

可以在 https://www.dcl.hpi.uni-potsdam.de/home/loewis/table-3131.html .

Python 2

Python语言参考,§2.3,标识符和关键字"

标识符(也称为名称)由以下词汇定义描述:

Identifiers (also referred to as names) are described by the following lexical definitions:

identifier ::=  (letter|"_") (letter | digit | "_")*
letter     ::=  lowercase | uppercase
lowercase  ::=  "a"..."z"
uppercase  ::=  "A"..."Z"
digit      ::=  "0"..."9"

标识符的长度是无限的.情况很重要.

Identifiers are unlimited in length. Case is significant.

这篇关于python类名称中的有效字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆