Idiosyncratic python

Todd toddrjen at gmail.com
Thu Sep 24 14:21:14 EDT 2015


On Sep 24, 2015 18:59, "Chris Angelico" <rosuav at gmail.com> wrote:
>
> 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.

True, but the code I saw doing this was just choosing between simple float
literals.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150924/92172716/attachment.html>


More information about the Python-list mailing list