Easy Questions
-
What is the Python Data Model?
-
Why does Python use
len(collection)
instead ofcollection.len()
?
Why is `len()` not a method in Python? -
What are "dunder" methods? Give an example.
-
What is the purpose of
__len__
in a Python class? -
How does
__getitem__
enable sequence-like behavior in a class? -
What is the difference between
__repr__
and__str__
? -
Why is
__repr__
important for debugging? -
How can you make an object iterable in Python?
-
What happens if you don’t implement
__bool__
in a class? -
What built-in function calls
__abs__
?
Medium Questions
-
Explain how
FrenchDeck
leverages the Python Data Model to support slicing and iteration. -
Why does
random.choice
work onFrenchDeck
without any extra methods? -
How does
__contains__
affect the behavior of thein
operator? -
What is the role of
collections.namedtuple
in theFrenchDeck
example? -
How would you modify
FrenchDeck
to make it shuffleable? -
Why should you avoid directly calling special methods like
__len__
? -
How does
__add__
enable vector addition in theVector
class? -
Why does
Vector.__bool__
checkbool(abs(self))
instead of justself.x
orself.y
? -
What is the difference between
__mul__
and__rmul__
? -
How does Python determine the truth value of an object if
__bool__
is not implemented?
Complex Questions
-
How does Python’s handling of
len()
differ for built-in types vs. custom classes? -
Explain the concept of the "metaobject protocol" in Python.
-
Why does the
Vector
class return a new instance in__add__
and__mul__
instead of modifyingself
? -
How could you extend the
Vector
class to support n-dimensional vectors? -
Discuss the trade-offs between using
abs(self)
vs.self.x or self.y
inVector.__bool__
. -
What are "reversed operators" (e.g.,
__radd__
), and when are they used? -
How does Python’s operator overloading compare to other languages like C++ or Java?
-
Why does the Python Data Model avoid arbitrary "magic" method names like
__foo__
? -
How could you implement a context manager using
__enter__
and__exit__
? -
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.
No comments:
Post a Comment