new.instancemethod __iter__

Martin Drautzburg Martin.Drautzburg at web.de
Sat Feb 6 23:53:53 CET 2010


Hello all

When I create an Object and set its __iter__ method from outside

s = Sequence #one of my own classes
s.__iter__ = new.instancemethod(f,s,Sequence)

I get different results, depending on whether I call


for x in y.__iter__():
    print x

or

for x in y:
    print x

The first case does what I expected, i.e. it iterates over whatever f()
yields. In the second case nothing is printed. I have the impression
that it still calls the original __iter__() method (the one defined at
the class level).

Why is that so?
How can I replace the __iter__() method so it does what I want.

The reason I want to do such things is I need to implement operations
similar to what itertools do. However I want my own classes and the
operations are only similar to itertools, but differ in significant
details.



More information about the Python-list mailing list