Adding None-aware operators to Python?
A PEP that has been around for a while, without being either accepted or rejected, was reintroduced recently on the python-ideas mailing list. PEP 505 ("None-aware operators") would provide some syntactic sugar, in the form of new operators, to handle cases where variables might be the special None value. It is a feature that other languages support, but has generally raised concerns about being "un-Pythonic" over the years. At this point, though, the Python project still needs to figure out how it will be governed—and how PEPs can be accepted or rejected.
None-awareness
The basic idea is fairly straightforward. Patterns like the following:
if x is None:
x = 42
could be replaced with a more compact, and possibly more readable, version:
x = x ?? 42
or the even more compact:
x ??= 42
So, two new operators would be defined. The first is "??", which
returns the
left-hand side if it is not None and the right-hand side
otherwise; importantly, it does not evaluate the right-hand side at all
unless the left side is None. As with other "augmented
assignment" operators (e.g. +=), "??=" simply applies the
?? operator to the two arguments and assigns the result to the
left-hand side.
In that same vein, None-aware indexing and attribute-access operators can change code like the following:
if x is not None and x['foo'] == 'bar':
...
if y is not None and y.foo == 'bar':
...
to:
if x?['foo'] == 'bar':
...
if y?.foo == 'bar':
...
The operators can, of course, be combined, as the following example from
the PEP shows. The first line is an expression from the
email/generator.py file in the standard library, while the second
is a rewrite using the new operators:
mangle_from_ = True if policy is None else policy.mangle_from_
mangle_from_ = policy?.mangle_from_ ?? True
Readers who think some or all of that looks more like Perl—or, more
pejoratively, like line noise—than Python are not alone. That is probably
one of the most common complaints heard after Steve Dower reintroduced the PEP in mid-July (as well as
in previous discussions of the PEP). In that
message, he noted that it might not be the best time to bring up the PEP, given
the fact that there was no agreed-upon way for the PEP to be
accepted (or, for that matter, rejected) since the recent resignation of
Guido van Rossum; "but since we're
likely to argue for a while anyway it probably can't hurt (and maybe this
will become the test PEP for whoever takes the reins?).
"
"Argue for a while" is what followed. Many were simply opposed to any of the operators, largely because of their supposed un-Pythonic nature. But some were more open to just adding ?? (and ??=); the None-aware indexing and attribute-access operators (?[] and ?., also known as "maybe-subscript" and "maybe-dot") can be strung together in ways that seem potentially confusing so folks seemed more wary of them. There are also differing opinions on the readability of the resulting "one-liners"—even advocates of the new operators do not seem inclined toward using them willy-nilly.
Spelling
Many of the arguments against the operators boil down to how they are "spelled"; this is the root of the "un-Pythonic/Perl-like/line-noise" argument. But that's only part of the discussion—or should be. If there is utility to replacing the canonical way to default a variable, how to spell the operator (or keyword) should be a secondary consideration; as Steven D'Aprano put it:
That's not to say that spelling is not important *at all*, or that we should never prefer words to symbols. But if the only objection we have is "this is useful but I don't like the spelling so -1" then that's usually a pretty weak argument against the feature.
Giampaolo Rodolà is concerned that most of the new operators are not "explicit", effectively arguing that they run aground on the "explicit is better than implicit" guideline in The Zen of Python. As he put it:
D'Aprano argued that any new operators are going to have to look somewhat
strange since all of the non-strange operators are already in use. Rodolà
said that was part of his point: "Not to state the obvious but it's
not that we *have to* use the remaining unused symbols just because
they're there.
" David Mertz went even
further, arguing that the PEP 572
approval has pushed things too far:
I was consistently +0 on the 572 idea, as long as its worst excesses were trimmed, as in the final PEP. But after reading this discussion, I almost reconsider that opinion since its social effect seems to be a move towards accepting wild and unnecessary changes that "might be useful" for a few unusual programming patterns.
The argument over the explicitness of the new operators spawned a huge sub-thread, where it was pretty clear that no minds were being changed. Rodolà argued that the implicit check for None used by the operators makes them non-explicit. On the other hand, D'Aprano argued that if the operators are defined to do a particular thing, then using them that way is explicit. The whole argument caused him to proclaim:
Nicholas Chammas observed that perhaps it was not a difference of explicit versus implicit, but instead related to something that C++ inventor Bjarne Stroustrup once said:
> For new features, people insist on LOUD explicit syntax.
> For established features, people want terse notation.
I think the "explicit vs. implicit" part of this discussion is probably better expressed as a discussion about "loud vs. terse" syntax. None of the operators in PEP 505 have implicit behavior, to the best of my understanding. It's just that the operators are new and have terse spellings.
Limbo
Along the way, Dower tried to refocus the discussion by presenting some of the arguments being made that he found lacking, along with a rebuttal of each. That apparently did not have the desired effect, as he bowed out of the discussion shortly thereafter:
For some, that may be just as well. Raymond Hettinger said that he was worried that it was not the
right time for a discussion of
PEP 505 as it might introduce "divisiveness when we most need for be
focusing on coming together
". Furthermore, he was not a fan of the
feature for a number of reasons:
This PEP is one step further away from Python reading like executable pseudo-code. That trait is currently a major draw to the language and I don't think it should get tossed away just to mitigate a minor irritant.
He also noted that other implementations of Python are having a hard time
keeping up with the "ferocious rate of change
" in recent
Python releases, so he thinks a moratorium on language changes makes sense.
He listed off a long string of features that have been
added and wondered how many folks—including CPython core developers—were on
top of all of them. "We've been putting major changes in faster than
anyone can keep up with
them. We really need to take a breath.
"
Informally, a moratorium is what the participants in a thread on the python-committers mailing list (where only core developers can post) have more or less recognized. There is a de facto moratorium because of a lack of a way to decide on a PEP, but, even once that logjam has cleared (currently planned to be completed early in 2019), it seems likely that any major changes will push out to Python 3.9 (currently targeted for 2021)—at the very least.
The discussion of PEP 505 may serve another purpose, as well. Discussions of PEPs in python-ideas are supposed to be more freewheeling and potentially fractious, but the scars of PEP 572 linger in many ways. This discussion may fuel the efforts to find other ways to discuss PEPs once they reach the point where they might be decided upon (and would typically then move to the python-dev mailing list). Presumably, discussions on python-ideas would stay the same, but what happens after that might be in for some changes—much like the Python project itself at this point.
| Index entries for this article | |
|---|---|
| Python | None |
| Python | Python Enhancement Proposals (PEP)/PEP 505 |
The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:
Note: you can avoid this step in the future by logging into your LWN account.
