[Python-ideas] Null coalescing operators

Steven D'Aprano steve at pearwood.info
Mon Sep 21 05:50:16 CEST 2015

On Sun, Sep 20, 2015 at 07:38:18PM +1000, Chris Angelico wrote:
> On Sun, Sep 20, 2015 at 5:31 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> > Technically, x.y x[y] and x(y) aren't operators, but for the sake of
> > convenience I'll call them such. Even though these are binary operators,
> > the ? only shortcuts according to the x, not the y. So we can call
> > these ?. ?[] ?() operators "pseudo-unary" operators rather than binary
> > operators.
> That's how all Python's short-circuiting works - based on the value of
> what's on the left, decide whether or not to evaluate what's on the
> right. (Well, nearly all - if/else evaluates the middle first, but
> same difference.) This is another form of short-circuiting; "x[y]"
> evaluates x, then if that's None, doesn't bother evaluating y because
> it can't affect the result.

I think you are mistaken about x[y]:

py> None[print("side effect")]
side effect
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable

That's why x?[y] is a proposal.


More information about the Python-ideas mailing list