Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Let's say I have a really long condition in python like

Python
if (ch >= 32 and ch <=64) or (ch >= 75 and ch <= 100) or ch = 106:
    ...


Let's say I wanted to break that condition onto multiple lines along the outer ors. How would I go about that? What is the proper indent level, or is it even possible?

I ask because I can't necessarily restructure this code. It's generated using a tool and that tool necessarily spits out really long test conditions sometimes. It's necessary. So I either have to do this using really long lines, or I need a way to break it up.

What I have tried:

I haven't tried anything yet, as I haven't installed Python yet. It's just a basic question about whitespace.
Posted
Updated 4-Nov-21 22:36pm

I'm no Python expert, but I think this could work:
Python
if (ch >= 32 and ch <=64) or \
   (ch >= 75 and ch <= 100) or \
   ch = 106:
    do_something()
There's also this Python Style Guide[^] to consult.
 
Share this answer
 
Comments
honey the codewitch 5-Nov-21 1:29am    
Are you sure it's not indented at the next level? My google fu isn't helpful right now, and I don't want to stop what I'm doing to start an install of that mess. Oh well. I'll figure it out one way or another. Thanks.
phil.o 5-Nov-21 2:57am    
Apparently, whenever you write if (, this opens a virtual space where indentation is relaxed. The link provided by Dave shows several options you have to get a proper syntax, no scrolling required, just a link right to a code block with examples.
You do not have to use backslashes for multiline conditions, as we would do with C preprocessor directives, for example.
honey the codewitch 5-Nov-21 3:08am    
Thanks, I totally blew by the link in the reply. It's the hour that it is. I'm not altogether with it right now. =)
phil.o 5-Nov-21 4:51am    
Who would be? :p
phil.o 5-Nov-21 3:04am    
Apparently, backslashes are not needed :)
I use a double indent so they are seen as still part of the if statement. Also put the expressions in parentheses:
Python
if (squares[row][0] != 0
        and squares[row][1] == squares[row][0]
        and squares[row][2] == squares[row][0]):
    draw_line(row, 0, row, 2, colour)
    return
 
Share this answer
 
Comments
honey the codewitch 5-Nov-21 5:02am    
That was my original plan for Python. I just wasn't sure if it was valid. Actually right now I think my multiline conditionals are indented once rather than twice but it's an easy fix in my generation template. the big issue is knowing what's valid, and secondarily what's pretty. I'm on the fence about double indenting multiline conditionals hanging off of ifs. It's prettier to corner case it, but I don't like it because it's not consistent with the general rule of indenting I follow through the rest of my code. It just rubs me wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900