Re: Combining colon statements on one line?

From: Aaron Bingham (bingham_at_cenix-bioscience.com)
Date: 07/16/04


Date: Fri, 16 Jul 2004 08:47:26 +0200
To: Dave Blackington <metapsyc@yahoo.com>

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?
>
>
Hi Dave,

Perhaps there are situations where a one-line if-else would win you
something, but I hardly see this as being such a situation.

Aren't you better off just defining a new function:

PIVOT_YEAR = 500
def millenialize(abbrYear):
    if x < PIVOT_YEAR:
        millenium = 2000
    else:
        millenium = 1000
    return millenium + abbrYear

Now, you only have those four lines in one place. Everywhere you need
the conversion will only take one line. Even if you only do the
conversion in one place, you've just made your program much clearer and
easier to change.

Regards,

-- 
--------------------------------------------------------------------
Aaron Bingham
Application Developer
Cenix BioScience GmbH
--------------------------------------------------------------------


Relevant Pages

  • 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)
  • 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 ... > correct millenium. ...
    (comp.lang.python)
  • Java OOP Help
    ... Im really new to programming and have just been given some work to do. ... Ive got a scenario that i have to pick stuff out of. ... to identify objects and classes and do a CRC card for each object. ... tips on the best way to identify them wud be great, ...
    (comp.lang.java.help)