File Naming Conventions in Python

Rana Hasnain Khan Oct 10, 2023
  1. Naming Convention for Functions in Python
  2. Naming Convention for Variable in Python
  3. Naming Convention for Class in Python
  4. Naming Convention for File in Python
  5. Method Naming Convention in Python
  6. Module Name in Python
  7. Global Variable Naming Convention in Python
File Naming Conventions in Python

We will introduce Python naming conventions and discuss how to use naming conventions in Python when class, variable, functions, object, file name, module, constant, package, global, and variable are concerned.

We can name variables in many different ways, and Python has made it easy. The Naming convention is essential in the programming language, making tasks easy and fast.

Naming Convention for Functions in Python

We can’t just name a function in Python. Some evident rules must be fulfilled while naming a function in Python.

While writing the Python function name, we should only use all lower case characters. No uppercase characters should be used.

When naming a function, it is permissible to use underscore(_) between the words as a substitute for space.

Code:

# python
def my_testFunction():
    print("This is a test function using underscore!")


def our_Criticfunction():
    print("This is a critic function.")


def newData_func():
    print("This is a better function name.")


my_testFunction()
our_Criticfunction()
newData_func()

Output:

function naming convention in python

Naming Convention for Variable in Python

It is the same as that of function names. Some rules are necessary to be followed when naming a Python variable.

The rules are as follows.

  1. It is mandatory to start a variable with an alphabet or underscore(_) character.
  2. A variable name should only be A-Z,a-z,0-9 and underscore(_).
  3. A number shouldn’t be the start of a variable name.
  4. Unique characters such as $,%,#,&,@.-,^, etc., are prohibited from using with the variable name.
  5. Variable names are case-related. For example, trs and Trs are two distinctive variables.
  6. Keywords like class, for, def, del, is else, try, and from should be avoided while naming a variable.

The following example is about which names are allowed in Python, as shown below.

# python
# Variable Names that are allowed
a = 2
b = "Hello"
pythonVariable = "Python Tutorial"
python_variable = "Python Tutorial"
_python_variable = "Python Tutorial"
_pythonVariable = "Python Tutorial"
PYTHONVARIABLE = "Python Tutorial"
pythonVariable = "Python Tutorial"
pythonVairbale3 = "Python Tutorial"

Let’s discuss the names that are not allowed in Python.

# python
# Variable Names that are not allowed
7pythonvariable = "Python Tutorial"
-pythonvariable = "Python Tutorial"
pythonv@riable = "Python Tutorial"
python variable = "Python Tutorial"
for = "Python Tutorial"

If we have used variables that are not allowed, it will show invalid syntax. It will process them one by one, and as a result, an error will appear.

Output:

variable naming convention in python

Naming Convention for Class in Python

The naming convention for classes in Python is identical to what it is for other programming languages, as there were rules in the case of variable and function. We also have to follow specific rules while naming classes in python as well.

The name determines the identity of anything. A unique name enhances your outlook.

So we should assign a proper name to a class as the program begins with the class. The rules are as follows.

  1. CamelCase convention should be followed.
  2. In the case of classes with the exception, while writing then, we should end the name with an Error.
  3. We can assign a class name like a function if we summon the class from someplace or callable.
  4. The classes within python are in lowercase.

Code:

# python
class OurClass
class HelloWorld
class UserError

Naming Convention for File in Python

When we are deciding on a name for your file, you need to keep the following rules in mind.

  1. A file name chosen should be a short name.
  2. All lowercase should be used when selecting a name for the file.
  3. The file name can also contain an underscore().

Method Naming Convention in Python

Naming a method in python, follow the rules below.

  1. When selecting a name for a method, you should choose all lowercase.
  2. To separate multiple words, if your method name had any, you should use an underscore(_).
  3. A name that is not for the public should begin with an underscore().
  4. Two underscores(_) should be used at the start when you wish to mangle a method name.

Constant Naming Convention in Python

Specific rules should be followed to name a constant in python.

  1. Always capitalize the constant name in Python.
  2. To separate multiple words, if your constant name had any, we should use an underscore(_).

Package Naming Convention in Python

Following are the rules that we should follow when naming a package.

  1. When selecting a name for a package, we should choose all lowercase.
  2. To separate multiple words, we should use an underscore(_) if our method name had any.
  3. It is beneficial to use a single word when naming a package.

Object Naming Convention in Python

When naming an object in python, we should follow the following rules.

  1. When selecting a name for an object, we should choose all lowercase.
  2. The name chosen should be very short.
  3. To separate multiple words, we should use an underscore(_) if our method name had any.

Module Name in Python

Some rules are necessary to be followed when you are naming a module in python.

  1. When selecting a name for a module, we should choose all lowercase.
  2. To separate multiple words, if your module name had any, you should use an underscore(_).
  3. It is beneficial to use a single word when naming a module.

Global Variable Naming Convention in Python

Following are the rules for naming a global variable.

  1. When selecting a name for a global variable, you should choose all lowercase.
  2. To separate multiple words, if your global variable had any, you should use an underscore(_).

Arguments of a Method

For instance, methods use self as the first argument. In the case of the class method, cls should be used as a beginning argument.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.

LinkedIn