Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Python have a "continue if" statement?

This is a pretty readable chunk of code I think,

for i in range(100):
    continue if i % 2 == 0

But it's not syntactically correct. We can do other nice things in Python like,

for i in things:
    total += 3 if i % 2 == 0 else 1

Or maybe,

return a if b > a else c

Why can't we do a continue if statement?

like image 800
chadlagore Avatar asked Mar 16 '18 01:03

chadlagore


People also ask

Can I use continue in an if statement Python?

Python Continue Statement You can use a continue statement in Python to skip over part of a loop when a condition is met. Then, the rest of a loop will continue running. You use continue statements within loops, usually after an if statement.

Can you use continue in a IF statement?

You can't use continue with if (not even a labelled one) because if isn't an iteration statement; from the spec. It is a Syntax Error if this ContinueStatement is not nested, directly or indirectly (but not crossing function boundaries), within an IterationStatement.

Why continue is not working in Python?

The SyntaxError: continue not properly in loop error is raised when you try to use a continue statement outside of a for loop or a while loop. To fix this error, enclose any continue statements in your code inside a loop.

How do you declare a continue in Python?

Example. #!/usr/bin/python for letter in 'Python': # First Example if letter == 'h': continue print 'Current Letter :', letter var = 10 # Second Example while var > 0: var = var -1 if var == 5: continue print 'Current variable value :', var print "Good bye!"


2 Answers

The flow:

for i in range(100):
    continue if i % 2 == 0

Would be equivalent to:

for i in range(1, 100, 2):
    ...

Or, more generically, to:

for i in range(100):
    if i % 2 == 0:
        continue

Python language designers have a history of voting against changes to the grammar which are only offering slightly different ways of doing the same thing ("There should be one obvious way to do it").

The type of one-liner construct which you've mentioned

x if cond else y

was an exception made, here. It was added to the language to offer a less error-prone way of achieving what many users were already attempting to achieve with and and or short-circuiting hacks (source: Guido). Code in the wild was using:

cond and x or y

Which is not logically equivalent, yet it's an easy mistake to make for users who were already familiar with the ternary cond ? : x : y syntax from C. A correct equivalent is:

(cond and [x] or [y])[0]

But, that's ugly. So, the rationale for the addition of an expression x if cond else y was stronger than a mere convenience.

like image 179
wim Avatar answered Oct 20 '22 01:10

wim


Because x if cond else y is actually an expression. Expressions are statements which evaluate to a value, in this case, either x or y.

continue is not a value, so there's that. Also,

if cond:
    continue

is really not much harder or more "error prone" than continue if cond, whereas v = x if cond else y is probably better than

if cond:
    v = x
else:
    v = y

There's also the fact that if we allowed continue if cond, we add a new way to use this _ if cond pattern, i.e. we allow it without an else.

For more info: https://docs.python.org/2.5/whatsnew/pep-308.html

like image 37
allidoiswin Avatar answered Oct 19 '22 23:10

allidoiswin