Python variable name prohibited characters

Valid variable names and naming rules in Python

In Python, identifiers (= variable names, function names, class names, etc.) need to be defined according to rules. Names that do not follow the rules cannot be used as identifiers.

The following is for Python 3 and may be different for Python 2.

Valid characters for identifiers (= names)

Here, the characters that can and cannot be used for identifiers (= variable names, function names, class names, etc.) are shown.

Basically, just remember the following.

  1. Uppercase and lowercase alphabets, numbers, and the underscore ( _ ) can be used.
  2. Numbers cannot be used as the first character.

Although non-alphabetic characters like Kanji can be used, it’s recommended to avoid them unless there is a compelling reason.

ASCII

ASCII characters that can be used for identifiers are uppercase and lowercase alphabets ( A — Z , a — z ), numbers ( 0 — 9 ), and the underscore ( _ ).

AbcDef_123 = 100 print(AbcDef_123) # 100 

Symbols other than the underscore cannot be used.

# AbcDef-123 = 100 # SyntaxError: can't assign to operator 

Numbers cannot be used as the first letter.

# 1_abc = 100 # SyntaxError: invalid token 

The underscore can be used for the first letter.

Note that the underscore at the beginning may have a special meaning.

Unicode

In Python 3, Unicode characters such as Kanji and Hiragana can be used.

変数その1 = 100 print(変数その1) # 100 

Not all Unicode characters can be used. For example, you cannot use emoji.

# ☺ = 100 # SyntaxError: invalid character in identifier 

See the official documentation for the Unicode category codes that can be used.

Check if the string is a valid identifier: isidentifier()

You can check whether a string is a valid identifier with the isidentifier() method of the string ( str ).

Читайте также:  Javascript when script is loaded

It returns True if the string is a valid identifier and False if not.

print('AbcDef_123'.isidentifier()) # True print('AbcDef-123'.isidentifier()) # False print('変数その1'.isidentifier()) # True print('☺'.isidentifier()) # False 

Words that cannot be used as identifiers: Reserved words and keywords

Reserved words and keywords are valid as identifiers but cannot be used as ordinary identifiers.

Note that isidentifier() returns True for reserved words and keywords since they are valid strings. However, using them as identifiers (variable names, function names, class names, etc.) will raise an error.

print('None'.isidentifier()) # True # None = 100 # SyntaxError: can't assign to keyword 

To get a list of keywords and check if a string is a keyword, use the keyword module of the standard library. See the following article.

Words that should not be used as identifiers

The names of built-in functions can be used as identifiers, so you can assign new values to them.

For example, len() is a built-in function that returns the number of elements in a list or the number of characters in a string.

If you assign a new value to the name len , the original function is overwritten. Note that no error or warning is printed when assigning.

print(len('abc')) # 3 len = 100 print(len) # 100 # print(len('abc')) # TypeError: 'int' object is not callable 

A common mistake is writing list = [0, 1, 2] , which prevents the use of list() .

For more information on checking the list of built-in functions and constants, see the following article.

Naming conventions (PEP8)

PEP stands for Python Enhancement Proposal.

PEP stands for Python Enhancement Proposal. A PEP is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment.
PEP 1 – PEP Purpose and Guidelines | peps.python.org

PEP8 describes the «Style Guide for Python Code».

Naming conventions are also mentioned.

Читайте также:  Проверка существования функции python

For example, the following styles are recommended. See the link above for details.

  • Module names
    • lowercase_underscore
    • lowercase
    • CapitalizedWords ( CamelCase )
    • lowercase_underscore
    • ALL_CAPS

    If your organization does not have its own naming conventions, it is recommended to follow PEP8.

    Источник

    What Unicode symbols are accepted in Python 3 variable names?

    I want to use a larger variety of Unicode symbols for variable names in my Python 3 scripts. What characters are acceptable to use in Python 3 variable names? I recently started using Unicode symbols (such as Greek and Asian symbols) for code obfuscation.

    That sounds like something you should cover with naming conventions, unless you can guarantee you’ll never have a maintainer or contributer who doesn’t understand one of the languages you use.

    I know that using odd symbols is not customary, but if we keep programming traditionally, then we keep get traditional programs. We need to think outside-of-the-box.

    1 Answer 1

    According to PEP 3131, the first character of an identifier needs to belong to ID_Start , the rest to ID_Continue , defined as follows:

    ID_Start is defined as all characters having one of the general categories uppercase letters (Lu), lowercase letters (Ll), titlecase letters (Lt), modifier letters (Lm), other letters (Lo), letter numbers (Nl), the underscore, and characters carrying the Other_ID_Start property. XID_Start then closes this set under normalization, by removing all characters whose NFKC normalization is not of the form ID_Start ID_Continue* anymore.

    ID_Continue is defined as all characters in ID_Start , plus nonspacing marks (Mn), spacing combining marks (Mc), decimal number (Nd), connector punctuations (Pc), and characters carryig the Other_ID_Continue property. Again, XID_Continue closes this set under NFKC-normalization; it also adds U+00B7 to support Catalan.

    Источник

    Can variable names in Python start with an integer?

    This is somewhat academic, but nevertheless. Python syntax forbids starting a variable name with a number, but this can be sidestepped like so:

    >>> globals()['1a'] = 1 >>> globals()['1a'] 1 

    Likewise for locals() . Does that mean that Python actually allows it, and that it’s just not very visible?

    Edit:

    My question is not whether it is allowed; I am aware that it is formally not allowed in Python. The question is why can I work around it by addressing globals() directly, and if that breaks certain rules or guidelines, or if it has a good reason/application to allow that.

    The parser disallows it, but you can do it by hijacking globals() . But you’re not really intended to do that.

    You can’t use it as a variable later. so it isn’t really a variable, even though it is in the same namespace.

    @steffan Modifying locals doesn’t work [docs.python.org/3/library/functions.html#locals] so you can only do this trick in globals

    2 Answers 2

    Python parser forbids naming variables that way, for the sake of parsing numbers and variables separately, as naming a variable 1e1 would create a chaos — is it the number 10.0 or the variable 1e1 ?

    «Python, please output for me 1e1 !» — «Why is it 10.0? I stored 100 over there!»

    But the variables are actually stored in a way that allows binding a string that starts with a number to a value, because that feature is no harm in hashing maps of any kind, and so using this «trick» you can achieve your wanted numeral-prefixed-name variable without hurting the parser severability.

    I would say that technically, naming variables in that manner is not a violation to python guidelines, but it is highly discouraged, and as a rule unnecessary. Using globals for injecting variables is known as a very bad practice and this case should not be an outstanding.

    Of course, python could have used an encloser to numerals like strings, say *123* , but I believe the intent of inventing python was to make programming easier, not stretching the limits of variable naming space.

    Practically speaking, if you must use number-headed names you better do it with your own dictionary, rather than globals :

    >>> number_headed_vars = >>> number_headed_vars['1a'] 100 

    That way you can create your own variables system — and avoid abusing globals() .

    Источник

Оцените статью