To make an object iterable in Python, you need to implement either:
✅ Option 1: __iter__()
The simplest and most common way is to define an __iter__() method that returns an iterator (usually self or a generator).
Example:
python
classMyIterable:
def__init__(self, data):
self.data = data
def__iter__(self):
returniter(self.data) # delegates to the iterable data
obj = MyIterable([1, 2, 3])
for item in obj:
print(item)
✅ Option 2: __getitem__() (legacy approach)
If your object implements __getitem__() and raises IndexError when the sequence ends, it is implicitly iterable.
Example:
python
classMyIterable:
def__init__(self, data):
self.data = data
def__getitem__(self, index):
return self.data[index]
obj = MyIterable([10, 20, 30])
for item in obj:
print(item)
Python internally tries to call obj[0], obj[1], ... until IndexError is raised.
🧠 Summary:
Method
Required?
Purpose
__iter__()
Yes
Returns an iterator for your object
__next__()
Optional
Needed if your class is also the iterator
__getitem__()
Legacy
Allows iteration without __iter__()
✅ Best practice: Prefer __iter__() + generators for clean, efficient code.
The __getitem__ method enables sequence-like behavior in a Python class by allowing you to access items in the object using square bracket notation—just like you do with lists, tuples, or strings.
✅ What it does:
When you define __getitem__(self, key) in your class:
Explain how FrenchDeck leverages the Python Data Model to
support slicing and iteration.
Why does random.choice work on FrenchDeck without
any extra methods?
How does __contains__ affect the behavior of the
in operator?
What is the role of collections.namedtuple in the
FrenchDeck 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 the
Vector class?
Why does Vector.__bool__ check bool(abs(self))
instead of just self.x or self.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 modifying self?
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 in Vector.__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.
Generated using DeepSeek (R1) from the chapter 1 of book "Fluent Python" by Luciano RamalhoTags: Technology,Python,Interview Preparation,
Have you ever feel that I don't know how to do with my life? Yeah? Actually, you're not alone. Most people feel like that kind of feeling. And when we look around the most like society and the world normally expecting or forcing us to have great purpose, meaning of life. When we discuss with our friends, family and workplace, in the society, looks like everybody is forcing us, you need to find your own purpose. But most people not. So why is that? So, in our meditation traditional what we call "everything is impermanent". And "everything is interdependent", meaning cause and condition. Like for example, some scientists they find the greatest discovery. But this discovery is not there when they begin to look for those, right? So you just kind of like have many different ideas. And you try, do some experiment. Then you find something. Then you go forward. And at the same time life is up and down because of impermanent. What we call life is like wave of the ocean. But the most important thing is, we are more than what we believe. So actually you have what we call everybody has this basic innate goodness. So you have awareness, love and compassion, wisdom, skill potential capacity. So, the most important is be present be with you right now. And then follow the flow of life with use your own wisdom. Your own love, compassion, skills, try your best. But don't you tie on the result. So then maybe you can find the better meaning of life rather than forcing yourself that I need to find a single meaning and the perfect that is difficult. When you force something that thing disappears, right? So be here now be present and believe in yourself and try your best. But don't tie on the result.