[Zope-dev] iterating over class without __iter__ but with __getitem__ raises AttributeError:__iter__

Hanno Schlichting hannosch at hannosch.eu
Mon Apr 13 05:22:31 EDT 2009


Chris Withers wrote:
> If you try and iterate over an instance of this class, you get an 
> AttributeError: __iter__. This doesn't make a lot of sense, since you 
> *don't* get an error like that if you iterate over an instance of:
> 
> class X:
>    def __getitem__(self,i):
>      return 1

The change you are interested in is probably:

http://svn.zope.org/Zope/trunk/lib/python/Acquisition/_Acquisition.c?rev=94905&r1=92577&r2=94905

Acquisition now proxy real iteration via __iter__ correctly (it didn't
do that before). What is missing from that change is probably the
fallback to the __getitem__ protocol, in case the class isn't a real
iterator.

Hanno



More information about the Zope-Dev mailing list