The "loop and a half"

Neil Cerutti neilc at norwich.edu
Wed Oct 4 08:33:32 EDT 2017


On 2017-10-04, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> ram at zedat.fu-berlin.de (Stefan Ram) writes:
>>Maybe this way: Use a while-loop and try-catch to get values
>>from an iterator until exhausted, and then introduce the
>>for-loop as an abbreviation for that.
>
> # while-loop
>
> iterable = range( 3 )
>
> iterator = iter( iterable )
> try:
>     while True:
>         i = next( iterator )
>         print( i )
> except StopIteration:
>     pass
>
> iterator = iter( iterable )
> try:
>     while True:
>         i = next( iterator )
>         print( i )
> except StopIteration:
>     pass
>
> # for-loop as-an-abbreviation
>
> iterable = range( 3 )
>
> for i in iterable:
>     print( i )
>
> for i in iterable:
>     print( i )

That looks the opposite of an excellent way to teach iterators.
If you insist they understand the iterator protocol and exception
handling first they're bound to think iteration is a hovercraft
full of eels.

-- 
Neil Cerutti




More information about the Python-list mailing list