Short if

Marc Boeren m.boeren at guidance.nl
Wed Jul 7 09:22:11 EDT 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
>       self.foo = (foo, someDefaultMutable)[foo is None]
>       self.bar = (bar, someOtherDefaultMutable)[bar is None]

Especially for a default value, I find the following construct better in
expressing what is happening:

    self.foo = someDefaultMutable
    if foo != None: 
        self.foo = 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