Idiosyncratic python

Chris Angelico rosuav at gmail.com
Thu Sep 24 18:57:54 CEST 2015


On Fri, Sep 25, 2015 at 2:54 AM, Todd <toddrjen at gmail.com> wrote:
> Using list indexing with booleans in place of a ternary operator.
>
> a = False
> b = [var2, var1][a]
>
> Instead of:
>
> b = var1 if a else var2

Be careful - these are not semantically identical. The first one
evaluates both var1 and var2, while the second will evaluate only the
one it needs. This might be significant if they're not simple names.

ChrisA


More information about the Python-list mailing list