Combining colon statements on one line?

D

Dave Blackington

Hi, I'm trying to figure out how to combine an 'if'
and 'else' statement on one line. Tonight I was
programming and came up with a very short if/else
scenario that demanded 4 lines due to the fact that
python can't read an 'if' & 'else' on the same line.
In order to do the same thing in 1 line, I needed to
make a dictionary. If you want to know the whole
scenario. Here it is:

'972' '006'

I have two dates without the millenium included.
Using basic 'if' 'else' communication, this, it seems
to me would require 4 lines of python to add the
correct millenium. These lines will also be so short
and waste a lot of blank space on the screen which
could be devoted to other commands in your function.
For digestion and organization purposes, it seems like
it helps to limit your functions (whenever possible)
to one screen. Taking up those 4 lines for such a
simple short 'if' 'else' seems a bit much, no?

I don't know if this is would be a desirable upgrade
for other python users or not, but I figured I would
mention it... assuming it's not already implemented.

Thanks, -Dave












__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail
 
S

Steven Bethard

Dave Blackington said:
'972' '006'

I have two dates without the millenium included.
Using basic 'if' 'else' communication, this, it seems
to me would require 4 lines of python to add the
correct millenium.

How about:

abbrYear < PIVOT_YEAR and 2000 or 1000

and's and or's will usually do the trick unless one of the result
values evaluates to false, e.g.

abbrYear < PIVOT_YEAR and 0 or 1000

would be bad because it would alway give you 1000. But in your case,
I think yo're okay.

Steve
 
J

Jeff Shannon

Dave said:
Hi, I'm trying to figure out how to combine an 'if'
and 'else' statement on one line. Tonight I was
programming and came up with a very short if/else
scenario that demanded 4 lines due to the fact that
python can't read an 'if' & 'else' on the same line.

You can also do this in only two lines, if that's really important to you.

if year < 500: year += 2000
else: year += 1000

Personally, I don't mind running a few extra lines. I can generally get
enough visible in my editor that it's not a problem -- functions/methods
that are more than a screen long are rather rare. And when I need to
see two more-widely separated functions at once... well, that's what
code folding is for. :)

Jeff Shannon
Technician/Programmer
Credit International
 
A

Alex Hunsley

Dave said:
Hi, I'm trying to figure out how to combine an 'if'
and 'else' statement on one line. Tonight I was
programming and came up with a very short if/else
scenario that demanded 4 lines due to the fact that
python can't read an 'if' & 'else' on the same line.
In order to do the same thing in 1 line, I needed to
make a dictionary. If you want to know the whole
scenario. Here it is:

'972' '006'

I have two dates without the millenium included.
Using basic 'if' 'else' communication, this, it seems
to me would require 4 lines of python to add the
correct millenium. These lines will also be so short
and waste a lot of blank space on the screen which
could be devoted to other commands in your function.
For digestion and organization purposes, it seems like
it helps to limit your functions (whenever possible)
to one screen. Taking up those 4 lines for such a
simple short 'if' 'else' seems a bit much, no?

Not really. If programmers were just obsessed with taking up as little
space as possible, we wouldn't have whitespace in our code. All the code
would fit in very little space but it would be very, very hard to read
and edit.
Minimising the length of the code isn't everything - readability (and
hence maintainability) counts for a lot.

Others have posted one liners that would do the trick but personally I'd
write it out with ifs for the sake of clarity...
alex
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top