[Python-ideas] PEP 505: None-aware operators
Michael Selik
mike at selik.org
Thu Jul 19 19:42:18 EDT 2018
On Thu, Jul 19, 2018 at 2:19 PM Terry Reedy <tjreedy at udel.edu> wrote:
> It seems to me that the problem is returning None.
If functions raised errors instead of returning None, we wouldn't have so
much trouble. Much of the benefit of PEP-572 was handling Nones and similar
sentinel values.
Instead of providing better tools for handling Nones, I'd rather encourage
folks to raise exceptions with a nice tool like PEP 463, exception-catching
expressions. Re-reading Guido's explanation for rejecting PEP 463, I now
understand the acceptance of PEP 572 better, as he also disagrees with the
preference for EAFP over LBYL.
For what it's worth, I agree with the folks that think ?. and ?[ create too
much visual clutter and are too easy to overlook or misunderstand. The ??=
and ?? operators will have spaces around them, so they're easier to read.
However, their benefit is so minor, if any, that it's better to stick with
the status quo.
I'd rather write one of these patterns:
# when x was assigned elsewhere
if x is None:
x = value
# when x is typically None
value if x is None else x
# when x is typically not None
x if x is not None else value
# when I'm playing code golf
x or value
In many of these cases I'd rather write 4 lines of code. It reads how I'd
speak the logic to another person, though out-loud I'd probably add a
"then" and say "otherwise" instead of "else":
if x is None:
y = a
else:
y = b
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180719/7b7dde12/attachment.html>
More information about the Python-ideas
mailing list