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

Chris Withers chris at simplistix.co.uk
Sun Apr 12 22:14:00 EDT 2009


Hi All,

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)

cheers,

Chris

PS: I have a f'ing horrible workaround for how:

...
     def __iter__(self):
         return SillyIterator(self)

class SillyIterator:
     def __init__(self,mnl):
         self.mnl = mnl
         self.i = 0

     def next(self):
         try:
             e = self.mnl[self.i]
         except IndexError:
             raise StopIteration
         self.i+=1
         return e
-- 
Simplistix - Content Management, Zope & Python Consulting
            - http://www.simplistix.co.uk


More information about the Zope-Dev mailing list