Named code blockes

Alex Martelli aleaxit at yahoo.com
Sun Apr 15 03:07:15 EDT 2001


"Steven D. Majewski" <sdm7g at Virginia.EDU> writes:

> Typically, a named block has the name label at the top -- which is
> not usually where the control is flowing to, and you have to hunt

Right: it's where the control is _breaking away from_ (for a break;
it IS, in fact, "where it's flowing to" for a continue).  Note that this
is even more obvious for the make-it-a-function alternative: return
clearly shows what (function) you are EXITING from, but NEVER
'where the control is flowing to' -- it flows to wherever it needs to,
that's not the point -- the point is terminating a conceptual unit
(and a named loop is as much as a conceptual unit as a function:
if it's code needs to occur exactly once, having it lexically inline
at the single point of occurrence is a plus!).

> and peek around to find the next executable statement. What's clear
> and explicit about that ?

A good part is in the name.
    break eggs_search
You don't need context to see that this is terminating the 'search
for eggs' named conceptual unit.  "what happens next" ("where
the control is flowing to") is obviously "whatever's next after
searching for eggs", just as if this was a function named that way
with a return statement here == indeed, the lexical situation in
terms of "let me look for where this goes now" is easier than for 
function calls, where it's generally anything but clear at a first
glance that a certain function is called from just one place, and
where that place is.


> A restricted use of GOTO is more (IMHO) clear. 

MHO differs.  I don't want to think of this-then-that-then-the-
other control flow in a VHLL, I want to think of named building
blocks and their nesting and entering and exiting.


> try/except blocks come in somewhere in between Goto and named
> blocks for readability. Not a bad compromise if you don't want
> to rely on disciplined use of goto. The target is pretty clear.

It's lexically constrained, but lacks a name, which is a strong
conceptual help (the name of the exception you raise can help,
but that should be the reason for exiting, not WHAT you are
exiting).

> ( But maybe you're thinking of a different syntax for Python 
>   than I can imagine from other lang. I've seen. If you show
>   me a clear example, I'll reconsider. ) 

Nope, I had in mind the same kind of syntax seen in other
languages, sugary issues apart (it's minor from my POV whether
naming a loop involves using 'as', a colon, or whatever) -- being
able to name a block and to mention that name in a break.


Alex



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com





More information about the Python-list mailing list