control structures (was "Re: Sins")
Emile van Sebille
emile at fenx.com
Wed Jan 5 18:22:56 EST 2000
Doesn't this do that now? or is there some objection to this usage?
for i in range(3):
try:
elem = "... nested loops & stuff ..."
if i < 2:
raise "element found", elem
"... more stuff ..."
raise "search failed"
except "element found", e:
print "Found", e
except "search failed":
print "Not Found"
print 'looping on i=%d' % i
print 'e-o-f'
Found ... nested loops & stuff ...
looping on i=0
Found ... nested loops & stuff ...
looping on i=1
Not Found
looping on i=2
e-o-f
Process completed successfully
maybe-I'm-missing-the-obvious?-ly y'rs
Emile van Sebille
emile at fenx.com
-------------------
Evan Simpson <evan at tokenexchange.com> wrote in message
news:<s76uc120hu870 at corp.supernews.com>...
> Steve Holden <sholden at bellatlantic.net> wrote in message
> news:387358BB.E71EC4E7 at bellatlantic.net...
> > Phil Jensen wrote:
> > > loop until "element found" or "search failed":
> > > ...
> > > if blah1 == blah2:
> > > "element found"
> > > ...
> > > when "element found":
> > > ...
> > > when "search failed":
> > > ...
>
> I proposed a spelling for this a while back, and have even made
sporadic
> forays into the Python source to see if I can implement it myself. It
> extends 'try', and would look like this:
>
> try:
> ... nested loops & stuff ...
> break "element found", elem
> ... more stuff ...
> break "search failed"
> continue "element found", e:
> print "Found", e
> continue "search failed"
> print "Not Found"
>
> > Typically one would use enumerations rather that strings to
> > represent the loop states, to combine readability with efficiency.
>
> In this syntax, the strings are treated as labels (despite being
string
> literals grammatically) and are purely static, for both efficiency and
> error-checking. That is, "break STRING, exprlist" is a compile-time
> SyntaxError unless the string exactly matches the string in a
"continue"
> clause of an enclosing "try" and the expression list is
> assignment-compatible with the argument list of that same clause.
Once this
> matching is done, the string is discarded and the "break" converted
into an
> expression evaluation followed by a Jump. "continue" clauses compile
into a
> Jump to the end of the "try" followed by an assignment LHS (the target
of
> the "break" Jump) and the suite body.
>
> In many ways this is quite similar to raising an exception and
catching it
> in an "except" clause. The major difference is that everything is
totally
> resolved at compile time, and is lexically fixed.
>
> > > Whatever the loop is, its exit or exits have some "meaning"
> > > in the program, and this control structure encourages an
> > > element of Literacy by forcing them to be named.
>
> Exactly. With no penalty in speed or (compiled) code size, in this
case.
>
> Cheers,
>
> Evan @ 4-am
>
>
> --
> http://www.python.org/mailman/listinfo/python-list
>
>
More information about the Python-list
mailing list