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

Carl Meyer carl at oddbird.net
Mon Sep 28 22:05:15 CEST 2015


On 09/28/2015 01:57 PM, Guido van Rossum wrote:
> On Mon, Sep 28, 2015 at 12:53 PM, Carl Meyer <carl at oddbird.net
> <mailto:carl at oddbird.net>> wrote:
> 
>     On 09/28/2015 01:43 PM, Carl Meyer wrote:
>     [snip]
>     > I assume that the short-circuiting would follow the precedence
>     > order; that is, nothing with looser precedence than member and index
>     > access would be short-circuited. So, for example,
>     >
>     >     foo?.bar['baz'].spam
>     >
>     > would short-circuit the indexing and the final member access, translating to
>     >
>     >     foo.bar['baz'].spam if foo is not None else None
>     >
>     > but
>     >
>     >     foo?.bar or 'baz'
>     >
>     > would mean
>     >
>     >     (foo.bar if foo is not None else None) or 'baz'
>     >
>     > and would never evaluate to None. Similarly for any operator that binds
>     > less tightly than member/index access (which is basically all Python
>     > operators).
> 
>     For a possibly less-intuitive example of this principle (arbitrarily
>     picking the operator that binds next-most-tightly), what should
> 
>         foo?.bar**3
> 
>     mean?
> 
> 
> It's nonsense -- it means (foo?.bar)**3 but since foo?.bar can return
> None and None**3 is an error you shouldn't do that. But don't try to
> then come up with syntax that rejects foo?.bar**something statically,
> because something might be an object implements __rpow__.
> 
> And I still don't see why this "principle" would be important.

The only "principle" in question here is "nothing with looser precedence
than member and index access would be short-circuited," and you seem to
agree with it. I was just making sure that

    foo?.bar**3

couldn't possibly mean

   (foo.bar**3 if foo is None else None)

and I'm glad it couldn't.

Carl

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150928/3f79ecff/attachment.sig>


More information about the Python-ideas mailing list