[IronPython] mapping C# iterators to Python iterator

Dino Viehland dinov at exchange.microsoft.com
Mon Dec 3 18:58:43 CET 2007

To map these to .NET's IEnumerator/IEnumerable you just need to define __iter__ or __getitem__.  If you define __iter__ you should return something that is enumerable (implements IEnumerable or has a next method which we'll call until it throws to stop enumeration).  If you define __getitem__ we'll try and index from 0 to infinity and stop at an index out of range or stop iteration exception.

You explicitly mention "say for ArrayList object" and I'm not sure what that means - we won't convert an arbitrary enumerable into another type such as ArrayList.  But we will convert it one of the enumeration interfaces.  When we pass your object out it'll be wrapped in one of our PythonEnumerable/PythonEnumerator/ItemEnumerable/ItemEnumerator classes.

-----Original Message-----
From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of amit
Sent: Saturday, December 01, 2007 4:15 PM
To: users at lists.ironpython.com
Subject: [IronPython] mapping C# iterators to Python iterator


I want to know how the "Iterators" in C# can be mapped to those in Python.
What I tried was:

say for ArrayList object

    def __iter__(self):
        self.index = self.Count
        return self

    def next(self):
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return ??

If I am creating python classes dynamically how would I map
GetEnumerator() , MoveNext() , Current() , Reset()
to python Class so as to make it iterable inside python.


Users mailing list
Users at lists.ironpython.com

More information about the Ironpython-users mailing list