python - Python 函数名称中允许使用的字符

标签 python function

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

最佳答案

In Python 3 many characters are allowed :

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

每个角色的完整列表可以在 Unicode.org 上找到.


在 Python 2.x 中,它仅限于字母、数字和下划线。来自 the docs :

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

关于python - Python 函数名称中允许使用的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19482730/

相关文章:

python - 使用正则表达式 python 验证卡号

Python 生成器让我困惑

python - 为 Django 构建自动化

在 C 的 typedef 结构中调用函数

允许访问 [[Call]] 的 JavaScript 实现

python - 索引 - 使用 Pandas 匹配

python - 具有 Linux 和 Windows 开发环境的 Azure SQL Server 和 FreeTDS ODBC

javascript - 函数同名问题

Python 在函数内导入库以实现多线程

javascript - vue数据函数语法