定义以数字开头的函数名(在 Python 3 中)? [英] Defining a function name that starts with a number (in Python 3)?

查看:47
本文介绍了定义以数字开头的函数名(在 Python 3 中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建以下函数:def 3utr():do_something().但是,我收到了 SyntaxError.用三"替换3"可以解决问题.

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

我的问题是:

  • 为什么会出现语法错误?
  • 有没有办法在 Python 3 中让函数名以数字开头?

推荐答案

这是一个语法错误,因为语言规范不允许标识符以数字开头.所以在 Python 中不可能有以数字开头的函数名(它们是标识符).

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 语言参考

在 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 语言参考

这篇关于定义以数字开头的函数名(在 Python 3 中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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