Newbie question about formatting long conditionals

B

Barney Frank

I am writing my first application using Python, and there are a
couple points in code at which I need a fairly complex if-else block.
I've discovered that you can nest compound conditions in parentheses, but
I haven't found a way to break a really complex if condition up into
multiple lines.

For example, take this little fake snippet:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1 and b < 4 and (c == 2 or d == 4):
print "Conditions met!"
#############



... if the "if" condition gets too long and unweildy for a single
line, is there any way to format it along these lines?:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1
and b < 4
and (c == 2 or d == 4):
print "Conditions met!"
#############
 
J

James Henderson

Barney said:
I am writing my first application using Python, and there are a
couple points in code at which I need a fairly complex if-else block.
I've discovered that you can nest compound conditions in parentheses, but
I haven't found a way to break a really complex if condition up into
multiple lines.

For example, take this little fake snippet:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1 and b < 4 and (c == 2 or d == 4):
print "Conditions met!"
#############



... if the "if" condition gets too long and unweildy for a single
line, is there any way to format it along these lines?:

Put the whole condition in parentheses:

if (a == 1 and
b < 4 and
etc.):

HTH,
James
 
G

Grant Edwards

... if the "if" condition gets too long and unweildy for a single
line, is there any way to format it along these lines?:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1
and b < 4
and (c == 2 or d == 4):
print "Conditions met!"
#############

if a == 1 \
and b < 4 \
and (c == 2 or d == 4):
print "Conditions met!"


if (a == 1
and b < 4
and (c == 2 or d == 4)):
print "Conditions met!"
 
A

anton muhin

Barney said:
I am writing my first application using Python, and there are a
couple points in code at which I need a fairly complex if-else block.
I've discovered that you can nest compound conditions in parentheses, but
I haven't found a way to break a really complex if condition up into
multiple lines.

For example, take this little fake snippet:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1 and b < 4 and (c == 2 or d == 4):
print "Conditions met!"
#############



... if the "if" condition gets too long and unweildy for a single
line, is there any way to format it along these lines?:

#############
a = 1
b = 2
c = 3
d = 4

if a == 1
and b < 4
and (c == 2 or d == 4):
print "Conditions met!"
#############

It doesn't answer your question, but still...

I personally prefer to introduce additional variables (in all the
languages I write) that _explain_ what precisly the condition mean:

isTheWorldGoingToBlow = (a == 1) and (b < 4) ....

if isTheWorldGoingToBlow:

The reason is simple: usually such a long conditions are too complicated
for a human to understand without additional hint.

Just my 5 kopecks :),
anton.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top