DEV Community

loading...
Cover image for #Day20 - Naming Conventions in Python

#Day20 - Naming Conventions in Python

Rahul Banerjee
Comp Eng Student @uoft | My opinions are my own Add me on LinkedIn: https://www.linkedin.com/in/rahulbanerjee2699/
Originally published at realpythonproject.com ・2 min read

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
Enter fullscreen mode Exit fullscreen mode

The words are separated using an underscore. Each word starts with a lower case letter.

Pascal Case

StudentMarks
Enter fullscreen mode Exit fullscreen mode

Each word starts with an upper case. They are not separated using any separator.

Camel Case

studentMarks
Enter fullscreen mode Exit fullscreen mode

The first word starts with a lower case letter and the following words start with an upper case letter.

Kebab Case

student-marks
Enter fullscreen mode Exit fullscreen mode

Each word starts with a lower case and they are separated with hyphens.

Hungarian Notation

arrStudentMarks
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Classes

Classes should be named in "CamelCase" format.

class MyClass:
    def __init__():
        pass

class Dog:
    def __init__():
        pass
Enter fullscreen mode Exit fullscreen mode

Below is another example of importing classes from a Module

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
Enter fullscreen mode Exit fullscreen mode

Global (module-level) Variables

Global variables should follow snake_case

variable = 1
variable_one = 2

def func():
      global variable
      global variable_one
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode

Functions

Functions should also follow snake_case

def func():
    pass

def func_one():
    pass
Enter fullscreen mode Exit fullscreen mode

Constants

Constant names must in all uppercase.

PI = 3.14
CONSTANT = 10
Enter fullscreen mode Exit fullscreen mode

It is just a naming convention. Python doesn't support constants.

Discussion (1)

Collapse
arvindpdmn profile image
Arvind Padmanabhan

Nice summary. Newbies can read PEP8: python.org/dev/peps/pep-0008/
For more general intro to naming conventions, read devopedia.org/naming-conventions

Forem Open with the Forem app