0% found this document useful (0 votes)
3 views3 pages

Python Team Style Guide Essentials

The Python Team Style Guide provides a comprehensive framework for writing clean and maintainable Python code, emphasizing consistent naming conventions, formatting, and clear function responsibilities. It advocates for the use of type hints, proper error handling, externalized configuration, and a structured project layout to enhance readability and scalability. Additionally, it underscores the importance of testing standards to ensure code reliability and facilitate safe refactoring.

Uploaded by

pravielan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Python Team Style Guide Essentials

The Python Team Style Guide provides a comprehensive framework for writing clean and maintainable Python code, emphasizing consistent naming conventions, formatting, and clear function responsibilities. It advocates for the use of type hints, proper error handling, externalized configuration, and a structured project layout to enhance readability and scalability. Additionally, it underscores the importance of testing standards to ensure code reliability and facilitate safe refactoring.

Uploaded by

pravielan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python Team Style Guide

A practical, opinionated guide for writing clean, maintainable Python code as a team.

1. Naming Conventions
Consistent naming makes code predictable and easier to navigate.

Variables & Functions (snake_case)


def calculate_total_price(items: list[dict]) -> float:
total_price = 0.0

for item in items:


total_price += item["price"]

return total_price

Classes (PascalCase)
class UserAccount:
def __init__(self, user_id: int, email: str):
self.user_id = user_id
[Link] = email

Constants (UPPER_SNAKE_CASE)
MAX_RETRIES = 3
DEFAULT_TIMEOUT_SECONDS = 30

2. Formatting & Layout


Follow PEP 8 formatting rules to ensure consistency across the team.
if user.is_active:
send_welcome_email(user)
else:
[Link]("Inactive user attempted login")

Line breaks for long expressions


total_amount = (
base_amount
+ tax_amount
+ service_fee
- discount
)

3. Functions & Responsibilities


Each function should have a single, clear responsibility.
def validate_email(email: str) -> bool:
if "@" not in email:
return False

if "." not in email:


return False
return True

Early return to reduce nesting


def process_order(order: Order) -> None:
if order is None:
return

if not order.is_paid:
return

[Link]()

4. Type Hints & Data Models


Type hints improve readability, tooling, and refactoring safety.
from dataclasses import dataclass

@dataclass
class User:
id: int
name: str
is_active: bool

def get_active_users(users: list[User]) -> list[User]:


return [user for user in users if user.is_active]

5. Error Handling & Logging


Errors should be explicit, logged, and actionable.
try:
user_id = int(request_data["user_id"])
except ValueError:
[Link]("Invalid user_id received", exc_info=True)
raise

[Link]("User created", extra={"user_id": [Link]})

6. Configuration & Security


Configuration must be externalized and never hardcoded.
import os

DATABASE_URL = [Link]("DATABASE_URL")

with open("[Link]") as config_file:


config = [Link](config_file)

7. Project Structure
A consistent structure improves onboarding and scalability.
app/
■■■ api/
■ ■■■ [Link]
■ ■■■ [Link]
■■■ core/
■ ■■■ [Link]
■ ■■■ [Link]
■■■ models/
■ ■■■ [Link]
■■■ services/
■ ■■■ user_service.py
■■■ [Link]

8. Testing Standards
Tests protect behavior and enable safe refactoring.
def test_calculate_total_price():
items = [
{"price": 10.0},
{"price": 20.0},
]

assert calculate_total_price(items) == 30.0

Main entry-point guard


def main():
start_application()

if __name__ == "__main__":
main()

You might also like