BongoBinary

Naming conventions in python

By Aloyce Bahati7 months ago5 minutes read

bongobinary logo, python naming conventions

Naming conventions is the way of writting names for class , package , variable , constants  and objects in any programming language. This post will try to explore diffrent naming conventions which are available on python programming language.

 

1. Small case with underscore. ( For variable names and functions names)

in python variable names are always named with all lowercase words and if words are separated with underscore.

 

Bellow is an example on normal variables.

#How to provide names for regular variables

foo = 12
bar = "Hello world"
foo_bar = "hi now"

 

Example on functions:

#How to provide names for regular functions

def say_hello():
    print("hello world");

 

2. Camel Case (StartEach word with  capital letter, no use of underscore)

Example on how to provide name for class.

#How to provide names classes on python

class Animal:
    pass

 

How to apply these naming conventions in python.

 

a. Naming packages.

For package always use lowercase and single word if there are multiple words which is rare still dont separate them with underscore.

example:

#Naming packages

mymathpackage

 

b. Naming functions and methods.

For those who are unfamiliar with the difference between functions and method then this is the diffence. Methods are functions which are declared on the class while function is the general word for functions which usually not declared on the class. In naming both functions and method use the same naming technique to name functions and methods we normal use all lower case word separed with underscore.

 

#How to provide names for functions

def say_hello():
    print("hello world");


#Names for methods

class Animal:
    #method naming
    def eat(self):
        print("animal eats")

 

c. Naming modules

in python modules we normally use all lowercase words which are separated with underscore.

#Naming modules

awesome_module.py

 

d.  Naming class

in python class name are normally named starting with uppercase and all words start with uppercase

#Names for class

class Animal:
    def walk(self):
        print("Animal can walk")


class AnimalHabitat:
    def is_safe(self):
        print("The habitat is safe now")

 

In this short article my goals was to make you familiar with different naming conventions which are availlable on python and to show you how to utilize them in different cases and places.  Thanks for taking your time in reading the content of this post. please dont forget to subscribe to bongobinary youtube channel for more contents on programming tutorials and tips. See you next time.