❓ Python Language - Q&A¶
Pairs with Memory & Runtime and OOP & Language Concepts. Format and tag legend: Overview.
Q1. What is the GIL and what are its implications? The Global Interpreter Lock lets only one thread execute Python bytecode at a time. So threads don't speed up CPU-bound work - use
multiprocessingor native libs for that - but they're fine for I/O-bound work since the GIL is released during blocking I/O. Follow-ups: How doesasynciodiffer from threads for I/O?Med·MAANGQ2. Is Python pass-by-value or pass-by-reference? Neither exactly - it's pass-by-object-reference ("call by sharing"). Rebinding a parameter doesn't affect the caller, but mutating a mutable argument (list, dict) does. Immutable args (int, str, tuple) appear value-like because you can't mutate them in place. Follow-ups: Show a bug this causes. (mutating a shared default list.)
Med·MAANGQ3. Why is a mutable default argument dangerous? The default is evaluated once at function definition and shared across all calls, so
def f(x=[])accumulates state between calls. Usedef f(x=None)and create the list inside. Follow-ups: When is the default expression evaluated? (at definition, not call.)Med·MAANGQ4. Which Python types are mutable vs immutable? Mutable: list, dict, set, most objects. Immutable: int, float, str, tuple, frozenset, bytes. Only immutable (hashable) types can be dict keys or set members. Follow-ups: Why can't a list be a dict key? (unhashable - mutable.)
Easy·MAANGQ5. What is a generator and why use one? A function using
yieldthat produces values lazily, one at a time, without building the whole sequence in memory. Ideal for large or infinite streams and memory-efficient pipelines. Follow-ups: Difference between a generator and a list comprehension? (lazy vs eager.)Med·MAANGQ6. What is a decorator? A callable that wraps a function to add behavior (logging, caching, auth, timing) without changing its body.
@lru_cacheand@propertyare stdlib examples. It's syntactic sugar forf = decorator(f). Follow-ups: How would you write one that times a function?Med·MAANGQ7. What is a context manager? An object usable with
withthat guarantees setup and teardown (via__enter__/__exit__), even on exceptions - closing files, releasing locks, committing/rolling back transactions. The Pythonic answer to deterministic resource cleanup. Follow-ups: How do you write one withcontextlib?Med·MAANGQ8. How does Python manage memory / garbage collection? Primarily reference counting (objects freed when their count hits zero) plus a generational cycle collector to reclaim reference cycles that counting alone can't. Old generations are collected less often (most objects die young). Follow-ups: Why does reference counting alone leak cycles?
Hard·Q9.
isvs==?==compares values (calls__eq__);iscompares identity (same object in memory). Useisonly for singletons likeNone. Small-int and string interning can makeisdeceptively true - don't rely on it for value equality. Follow-ups: Why does256 is 256but257 is 257may vary? (small-int caching.)Med·MAANGQ10. What's the difference between a list and a tuple? A list is mutable and general-purpose; a tuple is immutable and often used for fixed, heterogeneous records or as dict keys. Tuples are slightly more memory-efficient and hashable. Follow-ups: When would you deliberately choose a tuple? (fixed record, dict key, safety.)
Easy·MAANGQ11. What are
*argsand**kwargs?*argscollects extra positional arguments into a tuple;**kwargscollects extra keyword arguments into a dict. They let functions accept a variable number of arguments and forward them. Follow-ups: How do you forward them to another function? (f(*args, **kwargs).)Easy·StartupsQ12. How would you speed up CPU-bound Python code? Move the hot work off the GIL: use
multiprocessing, native/C extensions or NumPy (which release the GIL), or a different runtime. Profile first to find the real hotspot rather than guessing. Follow-ups: Why won't threads help here? (GIL serializes bytecode.)Med·MAANGQ13. What is a shallow copy vs a deep copy? A shallow copy duplicates the container but shares the nested objects; a deep copy recursively duplicates everything. Mutating a nested object through a shallow copy affects the original - a common bug with lists of lists. Follow-ups: Which does
list(x)orx[:]give? (shallow.)Med·
Related: Memory & Runtime · Concurrency Q&A · OOP & Design Q&A