PEP-able? Expressional conditions

Paul Rubin http
Thu Sep 8 11:30:03 CEST 2005


Terry Hancock <hancock at anansispaceworks.com> writes:
> > Not the same at all.  It evaluates both the true and false results,
> > which may have side effects.
> 
> If you are depending on that kind of nit-picking behavior,
> you have a serious design flaw, a bug waiting to bite you,
> and code which shouldn't have been embedded in an expression
> in the first place.

Are you kidding?  Example (imagine a C-like ?: operator in Python):

   x = (i < len(a)) ? a[i] : None    # make sure i is in range

Where's the design flaw?  Where's the bug waiting to bite?  That's a
completely normal use of a conditional expression.  If the conditional
expression works correctly, this does the right thing, as intended.
If both results get evaluated, it throws a subscript out of range
error, not good.

Every time this topic comes up, the suggestions that get put forth are
far more confusing than just adding conditional expressions and being
done with it.



More information about the Python-list mailing list