Python COM iterator

Carsten Haese carsten at uniqsys.com
Tue Apr 17 21:01:19 EDT 2007


On Tue, 2007-04-17 at 16:54 -0500, Larry Bates wrote:
> Does anyone know if there is a way to make a Python COM object
> act like a proper iterator in VB/Delphi?

I don't use COM, VB, or Delphi, but Google turned up these two
references:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconUsingForEach.asp  http://17slon.com/blogs/gabr/2007/03/fun-with-enumerators-part-1.html

Judging from those links, an object is iterable in VB and Delphi (or as
they call it, it is enumerable) if it exposes a GetEnumerator method
that returns an enumerator. The enumerator in turn needs to expose a
MoveNext method and a Current property.

Assuming that the Enumerable and the Enumerator are allowed to be the
same object, you'll probably need code that looks something like this:

  class foo:
    _public_methods_=['GetEnumerator','MoveNext']
    # You'll need to figure out how to expose "self.Current"

    def __init__(self):
        self.numbers=[1,2,3,4,5,6,7,8]
        self.Current = None

    def MoveNext(self):
        try:
            self.Current = self.numbers.pop(0)
            return True
        except IndexError:
            return False

    def GetEnumerator(self):
        return self

Good luck,

Carsten.





More information about the Python-list mailing list