[Tutor] __getitem__

Christopher Spears cspears2002 at yahoo.com
Mon Jan 16 21:12:20 CET 2006


I understand that you can use __getitem__ as a hook to
modify indexing behavoir in a class.  That's why
__getitem__ not only affects [] but also for loops,
map calls, list comprehension, etc.  For loops, etc.
work by indexing a  sequences from zero to a higher
index until out-of-bounds is reached.  But why does
this work?

>>> class stepper:
...     def __getitem__(self, i):
...         return self.data[i]
...
>>> 'p' in X
True

What does 'in' have to do with indexing?


More information about the Tutor mailing list