[Python-Dev] PEP 343 - Abstract Block Redux

Shane Hathaway shane at hathawaymix.org
Sun May 15 02:07:02 CEST 2005


Brett C. wrote:
> Guido van Rossum wrote:
>>PEP 340 is still my favorite, but it seems there's too much opposition
>>to it, so I'm trying to explore alternatives; at the same time I
>>*really* dislike the complexities of some of the non-looping
>>counterproposals (e.g. Nick Coghlan's PEP 3XX or the proposals that
>>make every keyword associated with 'try' a method).
>>
> 
> 
> Nick's was obviously directly against looping, but, with no offense to Nick,
> how many other people were against it looping?  It never felt like it was a
> screaming mass with pitchforks but more of a "I don't love it, but I can deal"
> crowd.

PEP 340 is very nice, but it became less appealing to me when I saw what
it would do to "break" and "continue" statements.

    text = 'diamond'
    for fn in filenames:
        opening(fn) as f:
            if text in f.read():
                print 'I found the text in %s' % fn
                break

I think it would be pretty surprising if the break didn't stop the loop.

Here's a new suggestion for PEP 340: use one keyword to start a block
you don't want to loop, and a different keyword to start a block that
can loop.  If you specify the non-looping keyword but the block template
produces more than one result, a RuntimeError results.  Here is example
A, a non-looping block statement using "try":

    text = 'diamond'
    for fn in filenames:
        try opening(fn) as f:
            if text in f.read():
                print 'I found the text in %s' % fn
                break

In example A, the break statement breaks the "for" loop.  If the
opening() iterator returns more than one result, a RuntimeError will be
generated by the Python interpreter.

Here is example B, a looping block statement using "in", adapted from
PEP 340:

    in auto_retry(3, IOError) as attempt:
        f = urllib.urlopen("http://python.org/peps/pep-0340.html")
        print f.read()

Note that I introduced no new keywords except "as", and the syntax in
both cases is currently illegal.

Shane


More information about the Python-Dev mailing list