Wednesday, June 18, 2025

Interview Questions on "The Python Data Model" - Ch 1 - Fluent Python

To See All Articles About Technology: Index of Lessons in Technology

Easy Questions

  1. What is the Python Data Model?

  2. Why does Python use len(collection) instead of collection.len()?
    Why is `len()` not a method in Python?

  3. What are "dunder" methods? Give an example.

  4. What is the purpose of __len__ in a Python class?

  5. How does __getitem__ enable sequence-like behavior in a class?

  6. What is the difference between __repr__ and __str__?

  7. Why is __repr__ important for debugging?

  8. How can you make an object iterable in Python?

  9. What happens if you don’t implement __bool__ in a class?

  10. What built-in function calls __abs__?


Medium Questions

  1. Explain how FrenchDeck leverages the Python Data Model to support slicing and iteration.

  2. Why does random.choice work on FrenchDeck without any extra methods?

  3. How does __contains__ affect the behavior of the in operator?

  4. What is the role of collections.namedtuple in the FrenchDeck example?

  5. How would you modify FrenchDeck to make it shuffleable?

  6. Why should you avoid directly calling special methods like __len__?

  7. How does __add__ enable vector addition in the Vector class?

  8. Why does Vector.__bool__ check bool(abs(self)) instead of just self.x or self.y?

  9. What is the difference between __mul__ and __rmul__?

  10. How does Python determine the truth value of an object if __bool__ is not implemented?


Complex Questions

  1. How does Python’s handling of len() differ for built-in types vs. custom classes?

  2. Explain the concept of the "metaobject protocol" in Python.

  3. Why does the Vector class return a new instance in __add__ and __mul__ instead of modifying self?

  4. How could you extend the Vector class to support n-dimensional vectors?

  5. Discuss the trade-offs between using abs(self) vs. self.x or self.y in Vector.__bool__.

  6. What are "reversed operators" (e.g., __radd__), and when are they used?

  7. How does Python’s operator overloading compare to other languages like C++ or Java?

  8. Why does the Python Data Model avoid arbitrary "magic" method names like __foo__?

  9. How could you implement a context manager using __enter__ and __exit__?

  10. What are the performance implications of using special methods vs. direct method calls?


These questions cover fundamental concepts, practical implementations, and deeper design principles from the chapter, suitable for assessing a candidate’s understanding of Python’s Data Model.

Generated using DeepSeek (R1) from the chapter 1 of book "Fluent Python" by Luciano Ramalho Tags: Technology,Python,Interview Preparation,

No comments:

Post a Comment