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

Dieter Maurer dieter at handshake.de
Mon Apr 13 02:59:16 EDT 2009


Chris Withers wrote at 2009-4-13 03:14 +0100:
>The context for this is trying to get ParsedXML 1.5 running on Zope 2.12 
>under Python 2.5 (don't ask why!)
>
>Anyway, ParsedXML has a class:
>
>class ManageableNodeList(ManageableWrapper, DOMProxy.NodeListProxy,
>                          Acquisition.Implicit):
>     "A wrapper around a DOM NodeList."
>     meta_type = "Manageable NodeList"
>
>     # redefine to get back the [] syntax with acquisition, eh?
>     def __getslice__(self, i, j):
>         return self.wrapNodeList(self._node.__getslice__(i,j))
>
>     # redefine to get back the [] syntax with acquisition, eh?
>     def __getitem__(self, i):
>         return self.wrapDOMObj(self._node.__getitem__(i))
>
>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
>
>I'm wondering there's some ExtensionClass or similar weirdness happening 
>here?
>
>(It didn't used to happen under Zope 2.9/Python 2.4)

It does not go wrong with Zope 2.11/Python 2.4, neither.
Maybe, changes done for Python 2.5/2.6 compatibility broke something.

Here is a simpler script to check for problems:

from Acquisition import Implicit
class C(Implicit):
  def __getitem__(self, i): return self.l[i]
  l=[1,2,3]

c=C()
iter(c)
list(_)
c2=C().__of__(c)
iter(c2)
list(_)




-- 
Dieter


More information about the Zope-Dev mailing list