Python Identifiers – Learn to name variables in Python

In this TechVidvan’s Python article, we are going to learn about identifiers in Python. They are the basic building blocks of Python and we use them everywhere while writing programs. So, it’s important to understand everything about them.

We will see the rules to define identifiers, and all the best practices to follow while defining Python identifiers. Let’s start with the definition of identifiers.

What is Python Identifier?

“An identifier is a name given to an entity”.

In very simple words, an identifier is a user-defined name to represent the basic building blocks of Python. It can be a variable, a function, a class, a module, or any other object.

Naming Rules for Identifiers

Now you know what exactly identifiers are. So, how do we use them? We can’t use anything, there are some certain rules to keep in mind that we must follow while naming identifiers.

1. The Python identifier is made with a combination of lowercase or uppercase letters, digits or an underscore.

These are the valid characters.

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

Examples of a valid identifier:

  • num1
  • FLAG
  • get_user_name
  • userDetails
  • _1234

2. An identifier cannot start with a digit. If we create an identifier that starts with a digit then we will get a syntax error.

Example:invalid identifiers in python

3. We also cannot use special symbols in the identifiers name.

Symbols like ( !, @, #, $, %, . ) are invalid.

Example:identifiers in python

4. A keyword cannot be used as an identifier. In Python, keywords are the reserved names that are built-in in Python. They have a special meaning and we cannot use them as identifier names.python identifiers

If you want to see the list of all the keywords, then in your Python shell, type “help()” and then type “keywords” to get the list of all Python keywords.python reserved keywords

5. The length of the identifiers can be as long as you want. Of course, it can not be greater than the available memory, however, the PEP-8 standards rule suggests not to exceed 79 characters in a line.

Testing the Validity of Python Identifiers

Python has some helper functions that are useful when you are not sure whether a string is a keyword or a valid identifier.

1. To check whether a string is a keyword or not, we have a keyword module.

import keyword
print( keyword.iskeyword(“var”) )
print( keyword.iskeyword(“False”) )
print( keyword.iskeyword(“continue”) )
print( keyword.iskeyword(“count”) )

Output:

False
True
True
False

2. The str.isidentifier() function is used to check the validity of an identifier.

print( “name”.isidentifier() )
print( “#today”.isidentifier() )
print( “_12hello”.isidentifier() )
print( “8cellos”.isidentifier() )

Output:

True
False
True
False

Best Practices for Python Identifiers

Following the naming conventions are mandatory for everyone. But that’s not it!

The Python community has made a few more guidelines that are not compulsory but it is advised to follow some practices that are better for everyone in understanding things. Let’s see what these guidelines are.

1. Class names should start with a capital letter and all the other identifiers should start with a lowercase letter.

2. Begin private identifiers with an underscore (_). Note, this is not needed to make the variable private. It is only for the ease of the programmer to easily distinguish between private variables and public variables.

3. Use double underscores (__) around the names of magic methods and don’t use them anywhere else. Python built-in magic methods already use this notation. For example: __init__ , __len__ .

4. Double underscores are used only when you are dealing with mangling in Python.

5. Always prefer using names longer than one character. index=1 is better than i=1

6. To combine words in an identifier, you should use underscore(_). For example: get_user_details.

7. Use camel case for naming the variables. For example: fullName, getAddress, testModeOn, etc.

Reserved Classes of Python Identifiersreserved classes of python identifiers

Some classes in Python have special meanings and to identify them, we use patterns of leading and trailing underscores.

1. Single leading underscore (_*)

This identifier is used to store the result of the last evaluation in the interactive interpreter. These results are stored in the __builtin__ module. These are private variables and they are not imported by “from module import *”

2. Double leading and trailing underscores (__*__)

Only the system-defined names use this notation. They are defined by the interpreter and its implementations. It is not recommended to define additional names using this convention.

3. Leading double underscores (__*)

Class-private name mangling: These category names are used within the context of a class definition. They are re-written to use a mangled form and avoid name clashes between private variables of base and derived classes.

Summary

This is all about the Python identifiers.

To sum everything up, we understood how the basic building blocks, Python identifiers, are named. We discussed the rules to define an identifier and all the best practices that every good Python programmer follows. Also, we discussed the reserved classes in Python Identifiers.