Python Senior interview questions and answers

75 questions for a Python (Senior) interview - with correct answers, explanations and links to the docs.

  1. 1.How does CPython free most objects?
  2. 2.Why does the cyclic garbage collector use generations?
  3. 3.What risk comes with defining __del__ on a class?
  4. 4.When is weakref the right tool?
  5. 5.What are immortal objects in CPython 3.12+?
  6. 6.What is the difference between a data descriptor and a non-data descriptor?
  7. 7.How do plain functions become bound methods?
  8. 8.What is the difference between __getattr__ and __getattribute__?
  9. 9.What does __set_name__ give a descriptor?
  10. 10.What is a metaclass?
  11. 11.Which hook often replaces a metaclass for registering subclasses?
  12. 12.When do you need to override __new__ instead of __init__?
  13. 13.Which side effect does __slots__ have beyond saving memory?
  14. 14.What does dis.dis(func) show?
  15. 15.What did the specializing adaptive interpreter in 3.11 change?
  16. 16.What does the free-threaded CPython build (PEP 703) change?
  17. 17.On a free-threaded build, does counter += 1 from many threads stay correct?
  18. 18.Why can NumPy speed up with threads even on a GIL build?
  19. 19.Why is the fork start method risky in a multithreaded process?
  20. 20.What has to be true about arguments sent to a ProcessPoolExecutor?
  21. 21.What happens when an asyncio task is cancelled?
  22. 22.What does asyncio.TaskGroup add over gather?
  23. 23.How do you handle errors from an ExceptionGroup by type?
  24. 24.Why keep a reference to a task created with asyncio.create_task?
  25. 25.How do you limit an asyncio crawler to 10 concurrent requests?
  26. 26.What is the right way to call a blocking library from async code?
  27. 27.Why are contextvars used instead of threading.local in async code?
  28. 28.How can you find what blocks the event loop?
  29. 29.A service's memory grows steadily. Which tool shows where Python allocates it?
  30. 30.Why is @lru_cache on an instance method a memory risk?
  31. 31.Which profiler suits a production process you can't restart?
  32. 32.What happens when a list's append needs more capacity?
  33. 33.Why does hash("abc") differ between runs of Python?
  34. 34.How are modern CPython dicts laid out?
  35. 35.What does typing.ParamSpec let a decorator express?
  36. 36.What does PEP 695 syntax def first[T](items: list[T]) -> T: replace?
  37. 37.What is @typing.overload for?
  38. 38.What does typing.Self express as a return type?
  39. 39.What does @runtime_checkable on a Protocol allow?
  40. 40.What changes with deferred annotation evaluation in Python 3.14 (PEP 649)?
  41. 41.What do template strings (t"...", PEP 750) return?
  42. 42.Why is pickle.loads on untrusted data dangerous?
  43. 43.Why avoid subprocess.run(f"convert {name}", shell=True)?
  44. 44.Which module should generate password reset tokens?
  45. 45.What is the difference between a wheel and an sdist?
  46. 46.Why shouldn't a library pin exact dependency versions like requests==2.32.3?
  47. 47.What does python -X importtime app.py help with?
  48. 48.Why are import-time side effects in a library a problem?
  49. 49.How should a library tell users that a function will be removed?
  50. 50.What does __match_args__ on a class enable?
  51. 51.Why does case RED: in a match not compare with a constant RED?
  52. 52.What changed about functools.cached_property in 3.12?
  53. 53.How do you run a FastAPI app across several CPU cores?
  54. 54.Why must a DB connection pool be created after a worker process forks?
  55. 55.How do you prevent two requests from updating the same Django row with a lost update?
  56. 56.What is the risk of sending a Celery task inside a DB transaction?
  57. 57.Why should Celery tasks be idempotent?
  58. 58.When does an async Django view not help?
  59. 59.What does property-based testing with Hypothesis add over example tests?
  60. 60.Why use create_autospec or autospec=True when mocking?
  61. 61.Tests share a module-level cache and fail only in some orders. What's the best fix?
  62. 62.What does sys.getrefcount(obj) usually return compared to the real number of references?
  63. 63.What did per-interpreter GIL (PEP 684, 3.12) enable?
  64. 64.What is the status of CPython's JIT compiler as of 3.13 and 3.14?
  65. 65.What does add_note() on an exception do?
  66. 66.A hot loop calls obj.method millions of times. What micro-optimization does CPython 3.11+ make less useful?
  67. 67.When is moving a hot path to Rust (PyO3) or Cython worth it?
  68. 68.Why does timeit turn off garbage collection by default?
  69. 69.Why is mutating a list while iterating over it a bug?
  70. 70.How do you make sure all asyncio tasks finish cleanly on SIGTERM?
  71. 71.What does asyncio.timeout(5) do around a block?
  72. 72.Why can't you use the ORM from an async Django view directly?
  73. 73.What does uv mainly replace in a Python workflow?
  74. 74.What is the trade-off of strict mypy in an existing large codebase?
  75. 75.Why can deeply recursive code fail in Python where it works in other languages?