问题 Python函数名中允许的字符


除了字母字符,数字和下划线之外,Python函数名中是否还有其他允许的字符?如果是的话,他们是什么?


9485
2017-10-20 20:51


起源

去谷歌上查询: pasteur.fr/formation/infobio/python/ch02s03.html - BartoszKP
可能重复 python类名中的有效字符 - Ben
可能重复 Python中变量和函数名称的命名约定是什么? - tecmec
@danny常规!=允许。
@delnan你是对的。 - tecmec


答案:


不在Python 2.x.从 文档

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

在Python 3中它被扩展了

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*">

The Unicode category codes mentioned above stand for:

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 numbers
Pc - connector punctuations
Other_ID_Start - explicit list of characters in PropList.txt 
                 to support backwards compatibility
Other_ID_Continue - likewise

14
2017-10-20 20:54



指向任何版本的Python 3.x文档,答案是肯定的:)例如: docs.python.org/3.2/reference/lexical_analysis.html#identifiers - Jon Clements♦
@JonClements D'哦!编辑。 - Matt S