#Day20 - Naming Conventions in Python
Rahul Banerjee
Posted on April 10, 2021
Today we will discuss some Naming conventions in Python.
Different type of naming conventions
We will name the same variable in the different conventions. Let's assume, we are trying to name a list or array which stores the marks of students.
Snake Case
students_marks
The words are separated using an underscore. Each word starts with a lower case letter.
Pascal Case
StudentMarks
Each word starts with an upper case. They are not separated using any separator.
Camel Case
studentMarks
The first word starts with a lower case letter and the following words start with an upper case letter.
Kebab Case
student-marks
Each word starts with a lower case and they are separated with hyphens.
Hungarian Notation
arrStudentMarks
In this variable naming convention, we add the data structure at the beginning of the name.
Naming convention in Python
Packages
Use snake_case for naming packages
import streamlit
import pulp
import flask_sqlalchemy
Modules
Modules are the functions you import from a package. They also use snake_case
from streamlit import subheader, text, markdown, title
from datetime import datetime
from flask import jsonify
Classes
Classes should be named in "CamelCase" format.
class MyClass:
def __init__():
pass
class Dog:
def __init__():
pass
Below is another example of importing classes from a Module
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
Global (module-level) Variables
Global variables should follow snake_case
variable = 1
variable_one = 2
def func():
global variable
global variable_one
Methods
- Public method should be named using snake_case
- By convention, private methods should be starting with an underscore ('_')
class MyClass:
'''
This is a public method
'''
def public_method():
pass
'''
This is a private method
'''
def _private_method():
pass
Instance Variables
Similar to methods, public instances should be in snake_case and private instances should begin with underscores(_)
class MyClass:
pass
public_instance = MyClass()
_private_instance = MyClass()
Functions
Functions should also follow snake_case
def func():
pass
def func_one():
pass
Constants
Constant names must in all uppercase.
PI = 3.14
CONSTANT = 10
It is just a naming convention. Python doesn't support constants.
Posted on April 10, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.