Why and how "there is only one way to do something"?

Brian Cully bcully at gmail.com
Thu Dec 15 10:55:15 EST 2005


bonono at gmail.com wrote:
> C-programmer learning python :
>
> Hi, where is condition ? true : false
>
> someone prefer the if/else statement type:
>
> Can't you see that the following is much more readable, stupid(well not
> the exact word but tone in such a way like words of messy or elegant
> etc.)
>
> if condition:
>   true
> else:
>   false

Except that the latter isn't an expression, and thus can't be used
inline.

Where you can do: somevar = condition ? true : false

You have to do, instead:
if condition:
    somevar = true
else:
    somevar = false

It may not seem like a big deal to you, but this approach has a number
of problems, depending on what you're doing. When you're using the
ternary operator you avoid temporary variables, and if you use it a
lot, that's much less that you have to keep track of. It's embeddable
in other arbitrary code, so you can move it around as you need to, or
just keep the morass of side-effects down.

Readability isn't just line-by-line, but a whole work. if/else may work
well most of the time, but sometimes it's ugly, and even though it's
obvious, it doesn't necessarily make your code easier to read.




More information about the Python-list mailing list