else on the same line - howto
Duncan Booth
duncan at NOSPAMrcp.co.uk
Wed Oct 15 07:16:25 EDT 2003
Helmut Jarausch <jarausch at remove.igpm.rwth-aachen.de> wrote in
news:bmj859$iph$1 at nets3.rz.RWTH-Aachen.DE:
> k+= (dy >= 0 ? 1 : -1)
>
> I would like to write
> if dy >= 0 : k+= 1; else: k-= 1
>
> instead of
> if dy >= 0 : k+= 1
> else : k-= 1
>
> but I don't know the exact syntax for writing
> the 'else' clause onto the same line.
>
> Thanks for a hint,
Hint: does using a newline character really cause you that much heartache?
There is no Python syntax for writing the else on the same line. Learn to
live with it. Anyone who has to maintain your code will thank you for
laying it out cleanly instead of cramping everything up onto a single line.
The clearest thing here would be to extract your intention out into a
separate function. For example:
def direction(delta):
if delta >= 0: return 1
return -1
then use it:
k += direction(dy)
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list