Python naming conventions (Detailed Guide)

By: On:

In this python tutorial, we will discuss in detail on Python naming conventions. Like how to use naming convention in python in case of class, variable, functions, object, file name, module, constant, package, global, variable, etc.

In my last blog, we discussed How to split a string using regex in python.

We will also discuss the below topics here.


  • How to choose a function name in Python?
  • What are the variable naming conventions in Python?
  • How to name a class in python?
  • What should be my file name in python?
  • What should be a method name in python?
  • Constant naming convention in python
  • How to name a package in python?
  • Object naming convention in python
  • What should be my module name in python?
  • Global variable naming convention in python
  • What is Snake_case naming style Python?
  • Do not ignore the arguments of a method.

In python, there are certain rules we need to follow while naming any of the components. Let’s see one by one on that.

Python naming conventions

Naming conventions are very important and useful in any programming language. Here we will see various Python naming conventions that we should use while programming in Python.

How to choose a function name in Python?

There are certain rules we need to follow while naming a function in Python.


  • Rule-1: We should write the Python function name with all lower case characters.
  • Rule-2: Do not use uppercase character while naming a function in python.
  • Rule-3: Use underscore(_) in between the words instead of space while naming a function.

Example:

def my_function():
  print("This is the way we should use the function name")

def critical_function():
  print("Better function name")

def new_data():
  print("Better function name")

Variable naming convention in python or What are the variable naming conventions in Python?

Python naming conventions for variable names are same as function names.

There are some rules we need to follow while giving a name for a Python variable.

  • Rule-1: You should start variable name with an alphabet or underscore(_) character.
  • Rule-2: A variable name can only contain A-Z,a-z,0-9 and underscore(_).
  • Rule-3: You cannot start the variable name with a number.
  • Rule-4: You cannot use special characters with the variable name such as such as $,%,#,&,@.-,^ etc.
  • Rule-5: Variable names are case sensitive. For example str and Str are two different variables.
  • Rule-6: Do not use reserve keyword as a variable name for example keywords like class, for, def, del, is, else, try, from, etc.
#Allowed variable names

x=2
y="Hello"
mypython="PythonGuides"
my_python="PythonGuides"
_my_python="PythonGuides"
_mypython="PythonGuides"
MYPYTHON="PythonGuides"
myPython="PythonGuides"
myPython7="PythonGuides"
#Variable name not Allowed

7mypython="PythonGuides"
-mypython="PythonGuides"
[email protected]="PythonGuides"
my Python="PythonGuides"
for="PythonGuides"

It shows invalid syntax. It will execute one by one and will show the error.

Naming conventions for python variable

How to name a Class in python

Python naming conventions for classes are the same as any other programming languages like C#.net or C++.

There are certain rules we need to follow while we are deciding a name for the class in python.

The program really looks cool when you will give a proper name for the class because the Program starts with the class.

  • Rule-1: We need to follow the camelCase convention
  • Rule-2: When you are writing any classes for an exception then that name should end with “Error“.
  • Rule-3: If you are calling the class from somewhere or callable then, in that case, you can give a class name like a function.
  • Rule-4: The builtin classes in python are with lowercase.

Example:

class MyClass

class Hello

class InputError

What should be my file name in python?

There are certain rules we need to follow while we are deciding a name for the file in python.

  • Rule-1: We should choose a short name as a file name.
  • Rule-2: You should use all lowercase while deciding a name for a file.
  • Rule-3: We can also use underscore(_) with the file name.

What should be a method name in python?

There are certain rules we need to follow while we are deciding a name for the method in python.

  • Rule-1: You should use all lowercase while deciding a name for a method.
  • Rule-2: If there are multiple words in your method name then they should be separated by an underscore(_).
  • Rule-3: Non-Public method name should start with an underscore(_).
  • Rule-4: If you want to mangle a method name then in that case we can use two underscores(_) at the beginning.

Constant naming convention in python

There are certain rules we need to follow while naming a constant.

  • Rule-1: Constant name in python should be capitalized always.
  • Rule-2: If there are multiple words in your constant name then they should be separated by an underscore(_).

How to name a package in python?

There are certain rules we need to follow while naming a package.

  • Rule-1: You should use all lowercase while deciding a name for a package.
  • Rule-2: If there are multiple words in your method name then they should be separated by an underscore(_).
  • Rule-3: It is always better to use a single word for a package name.

Object naming convention in python

There are certain rules we need to follow while we are deciding a name for the Object in python.

  • Rule-1: You should use all lowercase while deciding a name for an Object.
  • Rule-2: Choose a very short name.
  • Rule-3: If there are multiple words in your object name then they should be separated by an underscore(_).

What should be my module name in python?

There are certain rules we need to follow while we are deciding a name for the module in python.

  • Rule-1: You should use all lowercase while deciding a name for a module.
  • Rule-2: If there are multiple words in your module name then they should be separated by an underscore(_).
  • Rule-3: It is always better to use a single word for a module name.

Global variable naming convention in python

There are certain rules we need to follow while naming a global variable.

  • Rule-1: You should use all lowercase while deciding a name for a Object.
  • Rule-2: If there are multiple words in your global variable name then they should be separated by an underscore(_).

What is Snake_case naming style Python?

This is a format If there are multiple words in python object name then they should be separated by an underscore(_).

When we are separating a compound word with underscore(_) instead of spaces tha is known as Snake_case naming style Python.

Example:

Hello_World

My_Instance_Name

Do not ignore the arguments of a method

  • Rule-1: Use argument named ‘self’ as their first argument in case of Instance methods.
  • Rule-2: For class methods, use ‘cls’ as their first argument.

Rules in general for python naming conventions

  • Rule-1: Always give a meaning full name for any of the python objects. Don’t give a name like x,y, or z, etc.
  • Rule-2: Don’t give space in between object name instead of using underscore(_) when there are more than one-word presents.
  • Rule-3: Better to use camelCase only when it makes sense or else try avoiding more use of camelCase.

You may like following below Python Tutorials:

Conclusion:

Python is the most popular open-source object-oriented programming language and it is easy to learn and syntax wise it is very simple.

We should write the function name with all lower case character.You cannot start the variable name with a number.

We need to follow the camelCase convention while naming a class.

You should use all lowercase while deciding a name for a method.Non Public method name should start with an underscore(_).

We should use all lowercase while deciding a name for a Object.

Start a variable name with an alphabet or underscore(_) character.


We should choose a short name as a file name and all lowercase character.

This python tutorial explains the below points:

  • How to choose a function name in Python?
  • What are the variable naming conventions in Python?
  • How to name a class in python?
  • What should be my file name in python?
  • What should be a method name in python?
  • Constant naming convention in python
  • How to name a package in python?
  • Object naming convention in python
  • What should be my module name in python?
  • Global variable naming convention in python
  • What is Snake_case naming style Python?
  • Do not ignore the arguments of a method.
  • Rules in general for python naming conventions.

1 thought on “Python naming conventions (Detailed Guide)”

Leave a Comment