Python Mutable Default Arguments — Data-Leak Gotcha
Intermittent personal data leaks? Python's mutable default args cause shared state at definition - avoid this gotcha that FAANG interviews test.
- Python interviewers test reasoning, not rote syntax: mutability, GIL, decorators, and generators appear in every senior round
- Mutable default args cause shared-state bugs — use None and create fresh defaults inside the function
- The GIL serialises CPU-bound threads — use multiprocessing for parallelism, threading for I/O
- Decorators wrap functions for cross-cutting concerns; @functools.wraps preserves metadata
- Generators yield lazily — use them for streaming data or infinite sequences to save memory
- Context managers (with statement) acquire/release resources cleanly; implement with __enter__/__exit__ or @contextmanager
Think of a Python interview like a driving test. The examiner doesn't just want to see you turn the wheel — they want to know you understand WHY you check mirrors, signal, and brake in that order. These 50 questions are the 'manoeuvres' every Python examiner tests. Know the reasoning behind each one and you'll pass confidently, not by luck.
Python interviews trip up even experienced developers — not because the language is hard, but because interviewers aren't just testing syntax. They're testing whether you understand memory management, mutability traps, the CPython internals that explain quirky behaviour, and whether you can apply the right tool for the right job under pressure. A candidate who can recite list comprehension syntax but can't explain WHY it's faster than a for-loop won't get the role.
This article exists because most interview prep resources give you a list of questions with shallow one-line answers. That's fine for a quiz, but it won't help you when the interviewer follows up with 'why does that happen?' or 'what would you use instead in a production system?' Every answer here explains the mechanism, not just the result — so you can handle follow-ups confidently.
By the end of this article you'll be able to answer all 50 questions with depth, spot the common traps interviewers deliberately set, write clean Python that demonstrates seniority, and walk into any intermediate-to-senior Python interview with genuine confidence rather than crossed fingers.
Memory Management: Mutability and Identity
One of the most frequent 'gotcha' questions in Python interviews concerns the difference between mutable and immutable objects. In Python, everything is an object. Immutable objects (like strings, ints, and tuples) cannot be changed after creation; any 'modification' actually creates a new object in memory. Mutable objects (like lists and dicts) can be changed in place. This distinction is critical when passing arguments to functions, as Python uses 'Pass-by-Object-Reference.'
Concurrency and the GIL (Global Interpreter Lock)
For senior roles, you must explain why Python threads don't speed up CPU-bound tasks. The Global Interpreter Lock (GIL) is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes at once. While this makes memory management simpler, it means for true parallelism in CPU-intensive work, you must use the multiprocessing module to bypass the GIL by using separate memory spaces.
Generators and Iterators: Lazy Evaluation in Action
Generators are functions that use yield to produce a sequence of values lazily. They pause execution at each yield, preserving state until the next value is requested via . This makes them memory-efficient for large datasets and infinite sequences. Iterators are objects that implement next()_ and _iter__()_. Python's _next__()for loop works by calling on the target and then iter() repeatedly until next()StopIteration is raised. Generators are a special case of iterators generated by functions with yield.
yieldfreezes the function frame and returns a value to the caller.- The generator object stores the frame and local state on the heap.
- Calling
un-freezes and continues until the nextnext()yieldor end. - This is exactly how async/await works in Python — it's syntactic sugar over generators.
Decorators: Wrapping Functions for Cross-Cutting Concerns
A decorator is a function that takes another function as argument, extends its behaviour, and returns a new function. Python's @decorator syntax is syntactic sugar for func = decorator(func). Common use cases: logging, authentication, caching, timing, validation. Critical detail: unless you use @functools.wraps, the decorated function loses its original __name__ and __doc__, which breaks introspection and tooling.
Context Managers and Resource Management
Context managers handle acquiring and releasing resources automatically using the with statement. Built-in examples: file I/O, database connections, locks. You can create custom context managers by defining __enter__ and __exit__ methods on a class, or by decorating a generator function with @contextlib.contextmanager. The __exit__ method receives exception type, value, and traceback — you can choose to suppress exceptions or log them.
close() calls leaks database connections.The Shared-State Disaster: Mutable Default Arguments
- Never use mutable objects (list, dict, set) as default argument values.
- Use None as the default and instantiate the mutable inside the function body.
- Always check default arguments during code review — this bug is nearly invisible in log files.
tracemalloc.start() and snapshot; diff snapshots to find leak — or inspect with gc.get_objects()id() to confirm shared stateKey takeaways
Common mistakes to avoid
5 patternsUsing mutable default arguments in functions
Confusing 'is' vs '==' equality
Failing to use context managers for file I/O or database connections
open() as f:' or 'with db_session() as conn:' to guarantee cleanup.Assuming threading speeds up CPU-bound tasks
Forgetting @functools.wraps on decorators
Interview Questions on This Topic
Explain the difference between `__str__` and `__repr__` in Python.
__repr__ aims to be unambiguous — ideally returning a string that could be used to recreate the object (e.g., Fraction(3, 4)). __str__ aims to be readable for end users. If only __repr__ is defined, __str__ falls back to it. Senior engineers always define __repr__ first, then __str__ when a pretty-print is needed.Frequently Asked Questions
That's Python Interview. Mark it forged?
3 min read · try the examples if you haven't