Today we will discuss some Naming conventions in Python.
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.
The words are separated using an underscore. Each word starts with a lower case letter.
Each word starts with an upper case. They are not separated using any separator.
The first word starts with a lower case letter and the following words start with an upper case letter.
Each word starts with a lower case and they are separated with hyphens.
In this variable naming convention, we add the data structure at the beginning of the name.
Use snake_case for naming packages
import streamlit import pulp import flask_sqlalchemy
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 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 variables should follow snake_case
variable = 1 variable_one = 2 def func(): global variable global variable_one
- 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
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 should also follow snake_case
def func(): pass def func_one(): pass
Constant names must in all uppercase.
PI = 3.14 CONSTANT = 10
It is just a naming convention. Python doesn't support constants.