Nicojo
Nicojo

Reputation: 1083

Defining a function name that starts with a number (in Python 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.

My questions are:

Upvotes: 9

Views: 8149

Answers (2)

JamesNEW
JamesNEW

Reputation: 147

If you really want to be distinctive.

You can add '_' in front of an identifier

For instance

def _3utr():

Then call the function

_3utr()

Upvotes: 0

poke
poke

Reputation: 388053

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

Upvotes: 15

Related Questions