What are other use cases of this,
Conciseness (and therefore clarity, as soon as you get used to it, since after all it does not sacrifice readability at all!-) any time you need to check something and either use that something if it's true, or another value if that something is false (that's for and
-- reverse it for or
-- and I'm very deliberately avoiding the actual keywords-or-the-like True
and False
, since I'm talking about every object, not just bool
!-).
Vertical space on any computer screen is limited, and, given the choice, it's best spent on useful readability aids (docstrings, comments, strategically placed empty lines to separate blocks, ...) than in turning, say, a line such as:
inverses = [x and 1.0/x for x in values]
into six such as:
inverses = []
for x in values:
if x:
inverses.append(1.0/x)
else:
inverses.append(x)
or more cramped versions thereof.
and/or what is the rationale for this
rather unintuitive implementation?
Far from being "unintuitive", beginners regularly were tripped up by the fact that some languages (like standard Pascal) did not specify the order of evaluation and the short-circuiting nature of and
and or
; one of the differences between Turbo Pascal and the language standard, which back in the day made Turbo the most popular Pascal dialect of all times, was exactly that Turbo implemented and
and or
much like Python did later (and the C language did earlier...).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…