Re: Combining colon statements on one line?

From: Alex Hunsley (lard_at_tardis.ed.ac.molar.uk)
Date: 07/20/04


Date: Tue, 20 Jul 2004 00:03:39 GMT

Dave Blackington wrote:
> 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



Relevant Pages

  • Re: Combining colon statements on one line?
    ... > programming and came up with a very short if/else ... > scenario that demanded 4 lines due to the fact that ... > correct millenium. ... > for other python users or not, ...
    (comp.lang.python)
  • Re: Combining colon statements on one line?
    ... Dave Blackington wrote: ... >programming and came up with a very short if/else ... >scenario that demanded 4 lines due to the fact that ... see two more-widely separated functions at once... ...
    (comp.lang.python)
  • Re: Combining colon statements on one line?
    ... >programming and came up with a very short if/else ... >scenario that demanded 4 lines due to the fact that ... >I have two dates without the millenium included. ... the conversion will only take one line. ...
    (comp.lang.python)
  • Re: How to forcibly kill the word session?
    ... actually we are trying to cater to the generic scenario when word ... In a normal scenario,after doing a mailmerge, the ... Thus, i need a generic solution wherein, if the word hangs or is in a ... > be a nifty piece of programming. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: how do you terminate an application?
    ... not with End and programming ... the rest stays betwen me and my shrink. ... The scenario you are reffering to as "posted elsewhere" is considered ... (And then only to persons with "AAA" clearance). ...
    (comp.lang.basic.visual.misc)