docs on for-loop with no __iter__?

Steven Bethard steven.bethard at gmail.com
Sat Sep 4 14:18:05 EDT 2004


Can someone point me to the documentation on what's supposed to happen
when you use the "for x in X:" syntax when X does not have an __iter__
method?  I know that the code:

>>> class S:
...     def __len__(self): return 42
...     def __getitem__(self, i): return i
...
>>> for x in S():
...     print x

tries to print all the non-negative integers, starting with 0.  (Note
that the __len__ method doesn't stop it at 42.)  Obviously, the right
way to do this is with __iter__, but presumably this behavior is
documented somewhere...

Steve
-- 
You can wordify anything if you just verb it.
        - Bucky Katt, Get Fuzzy



More information about the Python-list mailing list