Re: Python equivalent for perl's "next"-statement?

  • Thread starter =?ISO-8859-1?Q?Gerhard_H=E4ring?=
  • Start date
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

Eric said:
Hello Pythonistas,

does anyone know an equivalent python statement for perl's
"next"-statement?
(Perl's "next" skips a loop cycle and continues with the next loop cycle
- it does not abort the whole loop).

It's the 'continue' statement.

-- Gerhard
 
H

Helmut Jarausch

Gerhard said:
It's the 'continue' statement.

Unfortunately, that's not the full truth.
In Perl the 'next' and 'last' instructions may refer
to a label of an (outer) loop and thus perform the action
for that specific outer loop.
Such possibilities are sadly missing in Python.
In the case of 'last' one can raise an exception,
while for 'next' I am not aware of an elegant solution.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
H

Helmut Jarausch

Gerhard said:
It's the 'continue' statement.

Unfortunately, that's not the full truth.
In Perl the 'next' and 'last' instructions may refer
to a label of an (outer) loop and thus perform the action
for that specific outer loop.
Such possibilities are sadly missing in Python.
In the case of 'last' one can raise an exception,
while for 'next' I am not aware of an elegant solution.


--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
J

John J. Lee

Helmut Jarausch said:
Gerhard Häring wrote: [...]
Unfortunately, that's not the full truth.
In Perl the 'next' and 'last' instructions may refer
to a label of an (outer) loop and thus perform the action
for that specific outer loop.
Such possibilities are sadly missing in Python.
In the case of 'last' one can raise an exception,
while for 'next' I am not aware of an elegant solution.

Python doesn't have a continue block (suite?) either, which would be
nice to have where you have continues in a loop (especially when there
are several of them) and you want to move your loop variable on to the
next in some sequence:

while condition(foo):
...
if hmm(): continue
...
if hmph(): break
...
if hrm(): continue
...
continue:
# Always called before while conditional evaluated
# (ie. either on continue or falling off the end of the loop body).
foo = foo**2


It's already a keyword, so no code breakage. And it makes more sense
in Python than it does in Perl, since it matches up with the current
meaning of continue (in Perl, a 'next' causes execution of the
'continue' block).

Without this, you have to scatter repeated function calls all over the
place:

def next_foo(foo):
return foo**2

while condition(foo):
...
if hmm():
foo = next_foo()
continue
...
if hmph(): break
...
if hrm():
foo = next_foo()
continue
....
foo = next_foo()


Yuck!


John
 
H

Hung Jung Lu

def next_foo(foo):
return foo**2

while condition(foo):
...
if hmm():
foo = next_foo()
continue
...
if hmph(): break
...
if hrm():
foo = next_foo()
continue
....
foo = next_foo()

Yuck!

Exception handling is often used for this purpose. (Common practice in
Python, believe it or not.)

class Next(Exception): pass

while condition(foo):
try:
...
if hmm(): raise Next
...
if hmph(): break
...
if hrm(): raise Next
...
except Next: pass
foo = foo**2

regards,

Hung Jung
 
J

John J. Lee

(e-mail address removed) (John J. Lee) wrote in message news:<[email protected]>... [...]
Exception handling is often used for this purpose. (Common practice in
Python, believe it or not.)

I believe you :)

class Next(Exception): pass

while condition(foo):
try:
...
if hmm(): raise Next
...
if hmph(): break
...
if hrm(): raise Next
...
except Next: pass
foo = foo**2

Yes, that's certainly much better, thanks for pointing that out. Even
better with the except: pass replaced with a finally:.

I still don't particularly like the gratuitous extra indentation
level, though, and I think continue: is more explicit (which was the
justification of finally:, of course).


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top