Identifiers in Python – Rules, Examples & Best Practices

Identifiers In Python

An Identifier is a user-defined name given to variables, functions, classes, modules, and other objects. As simple as it sounds, it is not. An identifier is not just any name, it helps to make code more readable and easier to understand by giving things relevant names. For this many rules have to be followed.

In this tutorial, we will learn the rules for writing identifiers, examples of valid and invalid identifiers, how to test whether a string is a valid identifier, and finally, we will understand best practices for naming identifiers.

Rules for Writing Identifiers in Python

A few naming conventions and rules must be followed when writing a Python identifier.

  • Keywords cannot be used as identifier names as they are reserved words in Python programming language. If you try, it will throw SyntaxError.
  • Python identifiers can contain letters in a small case (a-z), upper case (A-Z), digits (0-9), and underscore (_).
  • Identifiers cannot begin with a digit. For example, 10test would be an invalid identifier.
  • Python identifiers can’t contain only digits. For example, 888 would be an invalid identifier.
  • Python identifier names can start with an underscore. So, the _test would be a valid identifier.
  • There is no limit on the length of the identifier name. But, don’t try to keep a super long identifier, it will only hurt your credibility as a programmer.
  • Python identifier names are case-sensitive. So, “abc” and “ABC” are two different identifiers. It’s best to use lowercase letters for identifiers for uniformity across your programs.

Examples of Valid Identifiers in Python

Let’s look at some examples of valid identifiers in Python.

  • ab10c: Contains only letters and numbers
  • abc_DE: Contains all the valid characters
  • _: Surprisingly but yes, an underscore is a valid identifier
  • _abc: Identifier can start with an underscore

Examples of Invalid Valid Identifiers in Python

Let us now look at some examples of invalid identifiers in Python.

  • 99: Identifier can’t be only digits
  • 9abc: Identifier can’t start with the number
  • x+y: The only special character allowed is an underscore
  • for: It’s one of the reserved keywords in Python

How to Test if a String is a Valid Identifier?

We can use the string isidentifier() function to check if the identifier name is valid or not. But, this method doesn’t take reserved keywords into consideration. So, we can use this function with keyword.iskeyword() to check whether the name is valid.

print("abc".isidentifier())  # True
print("99a".isidentifier())  # False
print("_".isidentifier())  # True
print("for".isidentifier())  # True - wrong output

We know that “for” is a reserved keyword. So it’s not a valid identifier. Let’s define a function to test whether the identifier name is valid.

def is_valid_identifier(s):
    return s.isidentifier() and not keyword.iskeyword(s)

print(is_valid_identifier("for"))  # False
Python Invalid Identifiers Output
Python Identifiers

Best Practices for Naming Identifiers in Python

  • Class names should start with capital letters. For example Person, Employee, etc.
  • If the class name has multiple words, use Uppercase for the first character of each word. For example EmployeeData, StringUtils, etc.
  • You should use small letters for variables, functions, and module names. For example, collections, foo(), etc.
  • If variables, functions, and module names have multiple words then separate them with an underscore. For example, is_empty(), employee_object, etc.
  • For private variables, you can start their names with an underscore.
  • Avoid underscore as the first and last character in the identifier name. It’s used by Python built-in types.
  • If the identifier starts and ends with two underscores, then it means that the identifier is a language-defined special name, such as __init__. So you should avoid having two underscores at the start and the end of the identifier name.
  • Keep identifier names meaningful to clarify their intent. For example, phone_number, is_uppercase, etc.
  • If a function returns a boolean value, it’s better to start its name with “is”. For example, isidentifier, iskeyword, etc.
  • There is no limit on the length of the identifier name. But, keep it small and to the point. For example, the_employee_object_first_name can be better named as emp_first_name.

Summary

Keywords and identifiers are two essential concepts used in Python. A Python identifier is a name used as a variable name to enhance the readability and understanding of the code. In this tutorial, we have learned the rules of naming identifiers with examples of valid and invalid identifiers, which you must definitely keep in mind while writing identifiers on your program.

What’s Next?

Reference

https://docs.python.org/3/reference/lexical_analysis.html#identifiers