Defining a function name that starts with a number (in Python 3)?

岁酱吖の 提交于 2020-01-13 03:41:18

问题


I have tried creating the following function: def 3utr(): do_something(). However, I get a SyntaxError. Replacing the "3" by "three" fixes the problem.

My questions are:

  • Why is it a syntax error?
  • Is there a way to have a function name start with a number in Python 3?

回答1:


It is a syntax error because the language specification does not allow identifiers to start with a digit. So it’s not possible to have function names (which are identifiers) that start with digits in Python.

identifier ::= (letter|"_") (letter | digit | "_")*

Python 2 Language Reference

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 Language Reference



来源:https://stackoverflow.com/questions/13755532/defining-a-function-name-that-starts-with-a-number-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!