python3 脚本中可以使用哪些 unicode 字符?
Which unicode characters can be used in python3 scripts?
一些unicode字符可以用来命名变量、函数等,没有任何问题,例如α.
>>> α = "Hello world!"
>>> print(α)
Hello world!
其他 unicode 字符会引发错误,例如∇.
>>> ∇ = "Hello world"
File "<stdin>", line 1
∇
^
SyntaxError: invalid character '∇' (U+2207)
哪些 unicode 字符可用于构成 python 中的有效表达式?哪些 unicode 字符会引发 SyntaxError?
并且,是否有一种合理的方法来包含在 python 脚本中引发错误的 unicode 字符?我想在计算梯度的函数名称中使用∇,例如∇f表示f的梯度。
https://docs.python.org/3/reference/lexical_analysis.html#identifiers 说:
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 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.
让我们检查一下您的具体字符:
~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import unicodedata
>>> unicodedata.category('α')
'Ll'
>>> unicodedata.category('∇')
'Sm'
>>>
α 被归类为“字母,小写”,而 ∇ 是“符号,数学”。标识符中允许使用前一类,但不允许使用后一类。允许的字符类别的完整列表可在顶部的文档 link 中找到。
一些unicode字符可以用来命名变量、函数等,没有任何问题,例如α.
>>> α = "Hello world!"
>>> print(α)
Hello world!
其他 unicode 字符会引发错误,例如∇.
>>> ∇ = "Hello world"
File "<stdin>", line 1
∇
^
SyntaxError: invalid character '∇' (U+2207)
哪些 unicode 字符可用于构成 python 中的有效表达式?哪些 unicode 字符会引发 SyntaxError?
并且,是否有一种合理的方法来包含在 python 脚本中引发错误的 unicode 字符?我想在计算梯度的函数名称中使用∇,例如∇f表示f的梯度。
https://docs.python.org/3/reference/lexical_analysis.html#identifiers 说:
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 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.
让我们检查一下您的具体字符:
~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import unicodedata
>>> unicodedata.category('α')
'Ll'
>>> unicodedata.category('∇')
'Sm'
>>>
α 被归类为“字母,小写”,而 ∇ 是“符号,数学”。标识符中允许使用前一类,但不允许使用后一类。允许的字符类别的完整列表可在顶部的文档 link 中找到。