Identifiers in Python – Naming Rules & Best Practices

1. Objective

In our last Python tutorial, we discussed Python Subprocess Module. Today, in this Python tutorial, we will learn about identifiers in Python and how to name them. Moreover, we will see the rules, best practices, reserved classes in Python Identifiers. Also, we will test the validity of identifiers in Python. 
So, let’s start Identifiers in Python.

Identifiers in Python - Naming Rules & Best Practices

Identifiers in Python – Naming Rules & Best Practices

2. Ways to Define Identifiers in Python 

We can define identifiers in Python in a few ways:
“An identifier is a user-defined name to represent a variable, a function, a class, a module, or any other object.”
“It is a programmable entity in Python- one with a name.”
“It is a name given to the fundamental building blocks in a program.”
You must read about Python Tuples

3. Python Identifier Naming Rules

a. Rules in Identifiers in Python

So we know what a Python Identifier is. But can we name it anything? Or do certain rules apply? Well, we do have five rules to follow when naming identifiers in Python:
a. A Python identifier can be a combination of lowercase/ uppercase letters, digits, or an underscore. The following characters are valid:

  • Lowercase letters (a to z)
  • Uppercase letters (A to Z)
  • Digits (0 to 9)
  • Underscore (_)

Have a look at Python Number Types
Some valid names are:

  • myVar
  • var_3
  • this_works_too

b. An identifier cannot begin with a digit.
Some valid names:

  • _9lives
  • lives9

An invalid name:

  • 9lives
Identifiers in Python

Identifiers in Python – Naming Rules

c. We cannot use special symbols in the identifier name. Some of these are:
!
@
#
$
%
.

Identifiers in Python

Identifiers in Python – Naming Rules in Python

d. We cannot use a keyword as an identifier. Keywords are reserved names in Python and using one of those as a name for an identifier will result in a SyntaxError.

Identifiers in Python

Identifiers in Python – Identifiers Naming Rules

Identifiers in Python

Naming Rules in Python Identifiers

e. An identifier can be as long as you want. According to the docs, you can have an identifier of infinite length. However, the PEP-8 standard sets a rule that you should limit all lines to a maximum of 79 characters.
Do you know about Python Variables

b. Lexical Definitions in Python Identifiers

To sum those rules up lexically, we can say:
identifier ::= (letter | “_”) (letter | digit | “_”)*   #It has to begin with a letter or an underscore; letters, digits, or/and underscores may follow
letter ::= lowercase | uppercase #Anything from a-z and from A-Z
lowercase ::= “a” … “z” #Lowercase letters a to z
uppercase ::= “A” … “Z” #Uppercase letters A to Z
digit ::= “0” … “9” #Integers 0 to 9

4. Best Practices in Identifiers in Python

While it’s mandatory to follow the rules, it is also good to follow some recommended practices:

  • Begin class names with an uppercase letter, begin all other identifiers with a lowercase letter
  • Begin private identifiers with an underscore (_); Note that this doesn’t make a variable private, but discourages the user from attempting to access it
  • Put __ around names of magic methods (use leading and trailing double underscores), avoid doing this to anything else. Also, built-in types already use this notation.
  • Use leading double underscores only when dealing with mangling.

Let’s discuss Python Iterator

  • Prefer using names longer than one character- index=1 is better than i=1
  • Use underscores to combine words in an identifier, like in this_is_an_identifier
  • Since Python is case-sensitive, name and Name are two different identifiers.
  • Use camel case for naming. Let’s just clear the air here by saying camel case is myVarOne and Pascal case is MyVarOne.

5. Testing the Validity of Identifiers in Python

While it is great to follow the rules and guidelines, we can test an identifier’s validity just to be sure. For this, we make use of the keyword.iskeyword() function.
Have a look at Python Network Programming
The keyword module lets us determine whether a string is a keyword. It has two functions:

  • keyword.iskeyword(s)- If s is a Python keyword, return true
  • Keyword.kwlist- Return a sequence holding all keywords the interpreter understands. This includes even those that are active only when certain __future__ statements are in effect.

Coming back to iskeyword(s), it returns True if the string s is a reserved keyword. Else, it returns False. Let’s import this module.

>>> import keyword
>>> keyword.iskeyword('_$$_')

False

>>> keyword.iskeyword('return')

True
Also, the str.isidentifier() function will tell us if a string is a valid identifier. This is available since Python 3.0.

>>> '__$$__'.isidentifier()

False

>>> '__99__'.isidentifier()

True

>>> '9lives'.isidentifier()

False

>>> '9.5okay'.isidentifier()

False
Let’s discuss Python Flask

Python Interview Questions

6. Reserved Classes of Python Identifiers

Finally, let us talk about classes of identifiers. Some classes have special meanings and to identify them, we use patterns of leading and trailing underscores:

a. Single Leading Underscore (_*)

We use this identifier to store the result of the last evaluation in the interactive interpreter. This result is stored in the __builtin__ module. Importing a module as from module import * does not import such private variables.

b. Leading and Trailing Double Underscores (__*__)

These are system-defined names (by the interpreter). A class can implement operations to be invoked by special syntax using methods with special names. Consider this an attempt at operator overloading in a Pythonic fashion. One such special/ magic method is __getitem__(). Then, x[i] is equivalent to x.__getitem__(i). In the near future, the set of names of this class by Python may be extended.
Have a look at Python SciPy Tutorial

c. Leading Double Underscores (__*)

These are class-private names. Within a class definition, the interpreter rewrites (mangles) such a name to avoid name clashes between the private attributes of base and derived classes.
So, this was all in Identifiers in Python tutorial. Hope you like our explanation.

7. Conclusion – Identifiers in Python

Hence, in this Python Identifiers, we discussed the meaning of Identifiers in Python. Moreover, we learned naming rules and best practices in Python Identifiers. Also, we discussed reserved classes in Python Identifier. Still, if you have any doubt, ask in the comment tab.
See also – 
Python Charts
For reference

5 Responses

  1. Pushkar Piush says:

    >>> keyword.iskeyword(‘if’)
    True
    >>> ‘if’.isidentifier()
    True

    I have copied from Python IDLE and pasted …howcome it is showing true in both cases

    • DataFlair Team says:

      Hi Pushkar
      ‘if’.isidentifier() gives us True because keywords are indeed valid identifiers. You can test this with other keywords like ‘def’ and ‘for’.
      Hope, it helps.

  2. Semih says:

    Hello,
    import keyword
    keyword.iskeyword(“print”) it says False, but It must be True as I know.

    “print”.isidentifier() “this says True

    So whats different?

    • DataFlair Team says:

      Hello Semih,
      Thanks, for asking query regrading Python Identifiers.
      print() is a built-in function in Python 3. If you import keyword and check the list of available keywords:
      keyword.kwlist
      You get this:
      [‘False’, ‘None’, ‘True’, ‘and’, ‘as’, ‘assert’, ‘async’, ‘await’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘nonlocal’, ‘not’, ‘or’, ‘pass’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]

      This is why iskeyword() returns False for names like print and eval, but isidentifier() returns True, because they are indeed valid identifiers and we can actually use them as identifier names:
      print=32
      Hope, it helps!

  3. Semih says:

    Hello,
    Thank you a lot, it really helped! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.