[Tutor] _next

Dave Kuhlman dkuhlman at cutter.rexx.com
Tue May 30 22:20:21 CEST 2006


Roel Schroeven <rschroev_nospam_ml <at> fastmail.fm> writes:


[good suggestions snipped]

> 
> The _next() method is a generator function as I described above, which 
> creates an iterator object when called.
> The __iter__() method just calls that generator function and returns the 
> result to its caller.
> 
> HTH
> 

Kent Johnson alerted me to this discussion.

And, thanks, Roel, for the comments and improvements on my example.

I agree that my example class being discussed is confused.  I've reworked the 
example, taking hints from this thread.  And, while I was there, I've re-written 
the whole section on iterators.  Hopefully, I've made it less (rather than more) 
confusing.  You can find the new version here:

http://www.rexx.com/~dkuhlman/python_101/python_101.html
http://www.rexx.com/~dkuhlman/python_101/python_101.
html#SECTION004460000000000000000

You may be wondering why I would spend so much time on what seems to be a little 
used and advanced technique.  After all, iterators cannot be too important, 
since they did not even make it into Python until Python 2.2.  The answer I give 
myself is that this Python feature is very powerful and very elegant.  It also 
gives us the ability to write clearer and more maintainable code, for example, 
separating the producer and consumer parts of our code.  But, it does so only if 
we can work out a clear and unconfused way to explain and teach it.  Thanks for 
motivating me to try to do that.

Dave 





More information about the Tutor mailing list