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?
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.
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.
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.
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!"
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With