Is there a reason why this doesn't work in Python?
if 1 != 1 or
2 != 2:
print 'Something is wrong...'
You cannot split a statement into multiple lines in Python by pressing Enter . Instead, use the backslash ( \ ) to indicate that a statement is continued on the next line.
An implicit line continuation happens whenever Python gets to the end of a line of code and sees that there's more to come because a parenthesis ( ( ), square bracket ( [ ) or curly brace ( { ) has been left open.
Break a long line into multiple lines using backslash According to PEP8 coding convention, each line should be limited to maximum 79 characters for better readability.
Perhaps this prevents a grammar ambiguity, but I feel that this behaviour is in the spirit of PEP 20, specifically 'Simple is better than complex' (among others). In other words, 'Unless you have a good reason, why should expressions span multiple lines?'. If you have a good reason, the syntax devices to get around this are provided.
[edit] I did some more reading, and there are a few references of interest:
\
) continuation, but was rejected due to lack of support.
x = y+ # Used to be y+1, the 1 got dropped
f(x)
My final point is, the slash acts (or open parens) acts as a reminder that the statement is continued on the next line. Depending on your indentation, it's possible that the continuation could be mistaken for a separate statement (which I think this other response touches on).
Implicit line continuation only happens in Python if parentheses, brackets, or braces are open. Put parentheses around your condition and it will work.
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