Site icon DataFlair

Identifiers in Python – Naming Rules and Best Practices

Identifiers in Python

Python course with 57 real-time projects - Learn Python

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.

What are Identifiers in Python?

We can define identifiers in Python in few ways:

Python Identifier Naming Rules

1. Rules for naming 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:

Some valid names are:

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

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

An invalid name:

Identifiers in Python – Naming Rules

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

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 Naming Rules

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.

2. Lexical Definitions in Python Identifiers

To sum those rules up lexically, we can say:

Best Practices for Identifiers in Python

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

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.

The keyword module lets us determine whether a string is a keyword. It has two functions:

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('_$$_')

Output

False
>>> keyword.iskeyword('return')

Output

True

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

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

Output

False
>>> '__99__'.isidentifier()

Output

True
>>> '9lives'.isidentifier()

Output

False
>>> '9.5okay'.isidentifier()

Output

False

Reserved Classes of Python Identifiers

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

1. 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.

2. 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.

3. 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.

When you have completed the learning now, do not forget to take Quiz on Python Identifiers to check your knowledge. Also, attempt Interview Questions as below.

Python Interview Questions on Identifiers in Python

  1. What are identifiers in Python?
  2. What is a valid identifier in Python?
  3. Explain identifiers with example.
  4. What are reserved classes of Python Identifiers?
  5. Explain some of the rules for naming identifiers in Python.

Conclusion

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. 

Exit mobile version