Python COM iterator

Larry Bates larry.bates at websafe.com
Tue Apr 17 17:54:50 EDT 2007


Does anyone know if there is a way to make a Python COM object
act like a proper iterator in VB/Delphi?

Example:

Python COM object

class foo:
    _public_methods_=['next']

    def __init__(self):
        self.numbers=[1,2,3,4,5,6,7,8]
	
    def next(self):
        try: return self.numbers.pop(0)
        except IndexError:
            #
            # Normally in Python I would do this, but that raises a
            # COMexception when used in a COM object.
            #
            raise StopIteration

    def __iter__(self):
        return self


I want to be able to write something like (VB):

    oFOO=foo()
    for each n in oFOO
        '
        ' Do something with n
        '
    next

Seems like there should be a way.  Hope explanation is clear enough.

Regards, Larry




More information about the Python-list mailing list