Short if

Peter Hansen peter at engcorp.com
Wed Jul 7 10:00:33 EDT 2004


Marc Boeren wrote:

> 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.

In the case where you don't expect arguments to be passed
in which evaluate as False, the following is simpler:

self.foo = foo or someDefaultMutable
self.bar = bar or someOtherDefaultMutable



More information about the Python-list mailing list