Short if

Marc Boeren m.boeren at
Wed Jul 7 15:22:11 CEST 2004

> The readability concern is justified. Still, I find myself 
> using this style
> a lot, especially in situations such as:
> class Klass:
>   def __init__(self, a, b, foo=None, bar=None):
>       self.a = a
>       self.b = b
> = (foo, someDefaultMutable)[foo is None]
> = (bar, someOtherDefaultMutable)[bar is None]

Especially for a default value, I find the following construct better in
expressing what is happening: = someDefaultMutable
    if foo != None: = foo

It also eliminates the need for an else clause, but it does introduce an
extra assignment if foo != None.

Cheerio, Marc.

More information about the Python-list mailing list