[IronPython] IEnumerator in beta 5

Paul Viola viola at microsoft.com
Thu Apr 20 01:16:57 CEST 2006


 

I have written some code (see below) to try to construct enumerators
which can be used in other .NET code.  I must be missing something here.
I have not been able to create a class that inherits from IEnumerator
and works in IP.

 

The first class le_python is a python style iterator... it works.

 

The second class le_ienumerator is code which is trying to construct an
IEnumerator.  Note it does not work.

 

The third class le_python_ienumerator is a wild stab at something that
might work (a python style iterator that inherits from IEnumerator).
Note,  even though the code is the same as the first class, it no longer
works because of the inheritance (interesting).

 

$ ./IronPythonConsole.exe -E

IronPython 1.0.2300 (Beta) on .NET 2.0.50727.42

Copyright (c) Microsoft Corporation. All rights reserved.

>>> import test

 

>>> [e for e in test.le_python([1, 2, 3])]

[1, 2, 3]

 

>>> [e for e in test.le_python_ienumerator([1, 2, 3])]

Traceback (most recent call last):

  File , line 0, in input##17

AttributeError: 'le_python_ienumerator' object has no attribute
'MoveNext'

 

>>> [e for e in test.le_ienumerator([1, 2, 3])]

Traceback (most recent call last):

  File , line 0, in input##18

AttributeError: 'le_ienumerator' object has no attribute 'get_Current'

>>> 

 

 

 

import System

import System.Collections

from sho import *

 

class le_python:

    def __init__(self,obj):

        self.obj = obj

        self.ctr = -1

 

    def __iter__(self):

        return self

 

    def next(self):

        if self.ctr >= self.obj.__len__()-1:

            raise StopIteration

        self.ctr = self.ctr+1

        return self.obj[self.ctr]        

 

class le_ienumerator(System.Collections.IEnumerator):

    def __init__(self,obj):

        self.obj = obj

        self.ctr = -1

 

    def Reset(self):

        self.ctr = -1

 

    def MoveNext(self):

        if self.ctr >= self.obj.__len__()-1:

            return False

        self.ctr = self.ctr+1

        return True

 

    def get_Current(self):

        return self.obj[self.ctr]

 

class le_python_ienumerator(System.Collections.IEnumerator):

    def __init__(self,obj):

        self.obj = obj

        self.ctr = -1

 

    def __iter__(self):

        return self

 

    def next(self):

        if self.ctr >= self.obj.__len__()-1:

            raise StopIteration

        self.ctr = self.ctr+1

        return self.obj[self.ctr]        

 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20060419/e96cc964/attachment.html>


More information about the Ironpython-users mailing list