PEP 308: A PEP Writer's Experience - PRO
pyth at devel.trillke.net
Mon Feb 10 16:24:40 CET 2003
Andrew Koenig wrote:
> Tim> I used to buy that, but I'm having a harder time now. I mean the
> Tim> "by coincidence" part, not the "if ... it breaks" part. As I
> Tim> said, there are over 100 uses of and/or in the standard library
> Tim> now, all I looked at were obviously safe at a glance, and I have
> Tim> yet to find real code in the library where something stronger
> Tim> than and/or would actually help.
> I'm not worried about the standard-library authors misusing the
> idiom. I'm worried about ordinary programmers reading such code
> and failing to realize that the idiom doesn't work all the time.
People using the ternary-op may easily fail to realize
that the idiom isn't the right one all the time. For example,
it might prevent people from learning about the specific pythonic
behaviour of and/or returning the "short-circuiting" object.
result = hasattr(obj, 'method') and obj.method()
is not obvious to them but
result = obj.method() if hasatttr(obj, 'method') else None
is (because people just "know" the ternary-op and disregard and/or)
and finally it's used in non-assignments because it works:
if obj.method() if hasattr(obj, 'method') else None:
1) there are almost always more future users of a
language than current ones.
2) expressions can be used *anywhere* and thus have higher
potential of messing up readability than e.g. new statements.
More information about the Python-list