Python has set of rules for naming which helps the programmers to maintain consistency and readable code. This is essential for the collaborative projects where multiple programmers may need to use the same code base.

Python officially follow PEP 8 style guide which recommends a specific style for naming a variables, functions, classes and more. By using these guidelines, programmers can write their code clear and organized which would help fellow programmer understand quickly. Further it helps on tracking the errors and make update to the existing codes quickly.

Best Practices:

The general best practice for the naming are as followed

  • The variable and function names should be meaningful and represents a clear purpose.
  • Avoid using mixed-case names
  • Avoid using Python reserved keywords as variable names.
  • Avoid using generic names that convey a purpose for that variable i.e. data, numbers
  • Never use the characters ‘l’ (lowercase letter el), ‘O’ (uppercase letter oh), or ‘I’ (uppercase letter eye) as single character variable names.

Naming methods used in Python:


There are few naming methods mostly used in Python as below,

  • Snake Case - Separate each word with an underscore character (_)
  • Pascal case - Every word starts with an uppercase letter

We will see the standard naming method with few good and bad examples one by one

Files : Snake Case

Naming conventions are important for to maintain consistency across the projects. By following the standard rules for file naming, its helps the developers work collaboratively and makes the code base easier to maintain over time.

File name must be short, all-lowercase names. For example, a file for a calculator program might be named calculator.py. Using underscores in file names can enhance readability, especially for multi-worded concepts, although it’s generally preferable to avoid them.

Python also uses modules, which follow the same naming rules as files: lowercase and short. If necessary, underscores can be used to separate words.

The convention for packages is similar. Packages should also be short and lowercase. It’s common to use simple names so that other developers can easily understand the package’s purpose.

The main file in a Python package typically follows the convention of __init__.py. This file is important because it indicates that the directory should be treated as a package.

✅ Recommended

Python
# Use lowercase with underscores
data_processor.py
user_management.py
csv_file_handler.py
utils.py
constants.py

# Short and descriptive names
auth.py          # For authentication
models.py        # For data models
config.py        # For configuration
tests.py         # For tests

❌ Avoid

Python
# Don't use spaces or special characters
data processor.py    # Spaces not allowed
user-management.py   # Hyphens problematic
file@handler.py      # Special characters

# Don't use uppercase (except in special cases)
DataProcessor.py     # Hard to import
UserManagement.py    # Not conventional

Quick Reference

File Type Convention Example
Regular modules snake_case data_processor.py
Packages lowercase mypackage/
Test files test_ prefix test_models.py
Main scripts descriptive main.py, app.py
Config files descriptive config.py, settings.py
Executables descriptive manage.py, cli.py

Variables : Snake Case

    • Global variable - Uppercase with underscore
    • Local variables - Lowercase with underscore
Python
#✅ Good
#Local Variables 
user_name = "mithran"

#Global variable
PI = 3.14159
TOTAL_COUNT = 44

#❌Bad
x = 10 # Unless used in loop indices
username = "mithran"
UserName = "mithran"
user_Name = "mithran"

Function : Snake Case

    • Private function - These function supposed to be used internally inside a module/class. The naming format is snake case, but should start with '_'
Python
#✅ Good
# Functions
def hello_world():
    print("Hello World!")

def get_user_by_id(user_id):
    pass

#❌Bad
def HelloWorld():
    print("Hello World!")

def Get_User_By_Id(user_id):
    pass

Classes - Pascal case

Python
#✅ Good
class UserLogin:
    pass

class StudentsResult:
    pass

#❌Bad
class userLogin:
    pass

class students_result:
    pass

Quick Reference Table

Element Convention Example
Variables snake_case user_count
Functions snake_case get_user_data()
Classes PascalCase DatabaseConnection
Constants UPPER_SNAKE_CASE MAX_SIZE
Modules snake_case data_utils.py
Packages lowercase mypackage
Private _single_leading_underscore _internal_method()
Very Private __double_leading_underscore __private_var