[Python-ideas] PEP 505 (None coalescing operators) thoughts

Guido van Rossum guido at python.org
Mon Sep 28 20:38:38 CEST 2015


On Mon, Sep 28, 2015 at 10:38 AM, Carl Meyer <carl at oddbird.net> wrote:

> On 09/28/2015 11:29 AM, Guido van Rossum wrote:
> > On Mon, Sep 28, 2015 at 9:02 AM, Jeff Hardy <jdhardy at gmail.com
> > <mailto:jdhardy at gmail.com>> wrote:
> >
> >     -1 on the propagating member-access or index operators
> >
> >
> > Can someone explain with examples what this refers to?
>
> "Member-access or index operators" refers to the proposed ?. or ?[
> operators.
>

Got that. :-)


> "Propagating" refers to the proposed behavior where use of ?. or ?[
> "propagates" through the following chain of operations. For example:
>
>     x = foo?.bar.spam.eggs
>
> Where both `.spam` and `.eggs` would behave like `?.spam` and `?.eggs`
> (propagating None rather than raising AttributeError), simply because a
> `.?` had occurred earlier in the chain. So the above behaves differently
> from:
>
>     temp = foo?.bar
>     x = temp.spam.eggs
>
> Which raises questions about whether the propagation escapes
> parentheses, too:
>
>     x = (foo?.bar).spam.eggs
>

Oh, I see. That's evil.

The correct behavior here is that "foo?.bar.spam.eggs" should mean the same
as

    (None if foo is None else foo.bar.spam.eggs)

(Stop until you understand that is *not* the same as either of the
alternatives you describe.)

I can see the confusion that led to the idea of "propagation" -- it
probably comes from an attempt to define "foo?.bar" without reference to
the context (in this case the relevant context is that it's followed by
".spam.eggs").

It should not escape parentheses.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150928/fcedef44/attachment-0001.html>


More information about the Python-ideas mailing list