Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowed characters in Python function names

Are there any other allowed characters in Python function names except alphabetical characters, numbers, and underscores? If yes, what are they?

like image 448
Daniel Cook Avatar asked Oct 20 '13 20:10

Daniel Cook


People also ask

What is a valid function name in Python?

Valid names start with a letter or underscore but can include numbers. Words are lowercase and separated by underscores. It's important to know that function names can't be a Python reserved keyword. Then we have a set of opening and closing parentheses, () .

Can Python function names have spaces?

It change depend on the variable declare in the front of the script. All vaildation are done to prevent errors, crash, etc! And in the loop, it will call the function (with space). The function will need to link back to the array to change.


1 Answers

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

The complete list of every character can be found on Unicode.org.


In Python 2.x it was limited to just letters, numbers, and underscore. From the docs:

identifier ::=  (letter|"_") (letter | digit | "_")*
letter     ::=  lowercase | uppercase
lowercase  ::=  "a"..."z"
uppercase  ::=  "A"..."Z"
digit      ::=  "0"..."9"
like image 109
Matt S Avatar answered Oct 04 '22 04:10

Matt S