Identifier Naming Convention
Learn about the naming convention in Python.
We have created identifiers for many things—normal variables, functions, classes, instance data, instance methods, class data, and class methods.
It’s a good idea to adhere to the following conventions when creating identifiers.
Variables and functions not belonging to a class
All variables and functions not belonging to a class start with a lowercase alphabet.
For example: real
, imag
, name
, age
, salary
, printit()
, and display()
.
Variables that can be discarded
Use an underscore _
for variables to be used and discarded.
For example: for _ in [10, 20, 30, 40] : print(_)
.
Class names
These start with an uppercase alphabet.
For example: Employee
, Fruit
, Bird
, Complex
, Tool
, and Machine
.
Private identifiers
These are identifiers that we only want to be accessed from within the class in which they are declared. Start with two leading underscores __
.
For example: __name
, __age
, and __get_errors()
.
Protected identifiers
These are identifiers that we only want to be accessed from within the class in which they are declared, or from the classes that are derived from the class using a concept called inheritance. Start with one leading underscore _
.
For example: _address
and _maintain_height()
.
Public identifiers
These are identifiers that we want to be accessed from within the class or from outside it. Start with a lowercase alphabet.
For example: neighbor
and displayheight()
.
Language-defined special names
These start and end with two underscores __
.
For example: __init__()
, __del__()
, __add__()
and __sub__()
.
Note: Don’t call these methods. They are the methods that Python calls.
Unlike C++ and Java, Python does not have the private
, protected
, or public
keywords to mark the attributes. So if the above conventions are followed diligently, the identifier name itself can convey how it should be accessed.
Get hands-on with 1400+ tech skills courses.