For review: PEP 308 - If-then-else expression
James J. Besemer
jb at cascade-sys.com
Mon Feb 10 17:38:55 EST 2003
Andrew Dalke wrote:
> 4) is conducive to the development of a community guideline
> for when it is appropriate to use this operator vs. an if/else
> statement.
The pattern for using if-operator is actually fairly narrow (even asuming the
if/elif/else form). It applies when you have a need to select among several
(N) largely unrelated sub-expressions (some of which in general have
side-effects) selected by (N-1) boolean tests. The resulting intermediate
value then is used in concluding evaluation of the rest of the expression.
As with even the most trivial programming constructs, there is some "wiggle
room" where (depending on your view) some flexibility is allowed and/or
judgement is required.
> ("There should be one-- and preferably only one--obvious way to do it.")
This is a silly myth. It's just fine as anti-PERL rhetoric but it doesn't
hold beyond that.
In particular, Python doesn't come close to fulfilling to this ideal. In all
fairness, I don't think any real language can. So it's an unreasonable
constraint to try and apply to PEP 308.
E.g.,
for loop vs. while loop vs. comprehensions vs. map()
lambda() vs. def fn()
dict vs. list vs. class (for many abstractions)
etc., etc., etc.
Regards
--jb
--
James J. Besemer 503-280-0838 voice
2727 NE Skidmore St. 503-280-0375 fax
Portland, Oregon 97211-6557 mailto:jb at cascade-sys.com
http://cascade-sys.com
More information about the Python-list
mailing list