[Python-Dev] PEP 340: Breaking out.
Shane Hathaway
shane at hathawaymix.org
Wed May 4 21:02:40 CEST 2005
Alex Martelli wrote:
> Looking for a file with a certain magicnumber in its 1st two bytes...?
>
> for name in filenames:
> opening(name) as f:
> if f.read(2) == 0xFEB0: break
>
> This does seem to make real-life sense to me...
I'd like to suggest a small language enhancement that would fix this
example. Allow the break and continue statements to use a keyword,
either "for" or "while", to state that the code should break out of both
the block statement and the innermost "for" or "while" statement. The
example above would change to:
for name in filenames:
opening(name) as f:
if f.read(2) == 0xFEB0:
break for
This could be a separate PEP if necessary. When a "break for" is used
in a block statement, it should raise a new kind of exception,
BreakForLoop, and the block statement should propagate the exception.
When used outside a block statement, "break for" can use existing Python
byte code to jump directly to the next appropriate statement.
Shane
More information about the Python-Dev
mailing list