Question Bank

Python Language Trivia

Difficulty: Medium

Mid-tier Python gotchas: mutable default arguments, the GIL, method resolution order, and `is` vs `==`. Predict each snippet's output.

Question Bank
/

Python Language Trivia

Python Language Trivia

Mid-tier Python gotchas: mutable default arguments, the GIL, method resolution order, and `is` vs `==`. Predict each snippet's output.

Question Bank
Medium
Python
5 questions
py-gil
py-decorators
py-metaclasses
interview-prep

926 views

14

Predict the output of two consecutive calls. Explain the mutable-default-argument trap and the idiomatic fix.

Examples

Example 1:

Input: def append_to(value, target=[]); call append_to(1) then append_to(2)
Output (buggy version): [1], then [1, 2] (shared list)
Output (fixed version with target=None sentinel): [1], then [2]
Explanation: Default argument values are evaluated once at function definition time, not on each call, so target=[] is a single list object shared across all callers. The idiomatic fix uses None as sentinel and allocates inside the body.