Python · The language, not just syntax
What Python engineering practices do you consistently apply?
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.
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))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.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why is a mutable default argument still a common bug?
- 2Which checks do you enforce in CI and why those?
- 3When is a comment a sign the code should be refactored?
- 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.