Python · The language, not just syntax

What Python engineering practices do you consistently apply?

Hardbest practicescode qualitytoolingUpdated Oct 2026
Quiz this track

Short answer

Write small explicit functions, avoid mutable defaults, use context managers and type hints, keep dependencies pinned, and automate formatting, linting, typing and tests in CI.

Good Python code is boring in the best sense: obvious control flow, clear names and few surprises. PEP 8 and a formatter such as Ruff handle the mechanical style so reviews can focus on correctness, design and operability.

python
from dataclasses import dataclass

@dataclass(frozen=True)
class RetryPolicy:
    attempts: int = 3
    base_delay: float = 0.2

    def delay_for(self, attempt: int) -> float:
        return self.base_delay * (2 ** attempt)

def fetch_with_retry(client, url, policy=RetryPolicy()):
    for attempt in range(policy.attempts):
        try:
            return client.get(url)
        except TimeoutError:
            if attempt == policy.attempts - 1:
                raise
        time.sleep(policy.delay_for(attempt))
An immutable policy object replaces scattered constants. The retry loop is explicit about when it re-raises.

The non-negotiables

  • Never use mutable default arguments or mutable class attributes for per-instance state
  • Use context managers for anything that must be closed or released
  • Add type hints on public interfaces and run a checker in CI
  • Automate formatting and linting with Ruff; automate tests with pytest
  • Pin application dependencies and commit a lock file

Prefer explicit over implicit and simple over clever. A function that fits on one screen and does one thing is easier to test and review. When a piece of code needs a comment to explain what it does, first try renaming or restructuring it.

Consistency with the codebase beats personal preference. Agree on tooling in pyproject.toml so local runs and CI produce the same result.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1Why is a mutable default argument still a common bug?
  2. 2Which checks do you enforce in CI and why those?
  3. 3When is a comment a sign the code should be refactored?
  4. 4How do you balance consistency with improving legacy code?

Say it in the interview

I aim for boring, explicit code: small functions, clear names, no mutable defaults, and context managers for resources. I put type hints on public interfaces and automate Ruff, mypy or pyright, and pytest in CI so quality does not depend on review attention. I pin application dependencies and keep configuration in pyproject.toml. Consistency with the existing codebase matters, and I improve legacy areas incrementally rather than rewriting everything.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.