[Python-ideas] PEP 505: None-aware operators

Nicholas Chammas nicholas.chammas at gmail.com
Thu Jul 26 00:00:01 EDT 2018


On Wed, Jul 25, 2018 at 11:09 PM David Mertz <mertz at gnosis.cx> wrote:

> On Wed, Jul 25, 2018 at 10:50 PM Nicholas Chammas <
> nicholas.chammas at gmail.com> wrote:
>
>> Indeed. Thanks for the counter-example. I think the correct translation
>> is as follows:
>>     food = spam?.eggs?.bacon
>> Becomes:
>>     food = None
>>     if spam is not None and spam.eggs is not None:
>>         food = spam.eggs.bacon
>>
> Did I get it right now? :)
>>
>
> Nope, still not right, I'm afraid!
>
> Chris Angelica provided a more accurate translation.
>

Forgive me for being slow. I'm missing what's different in semantics
between the translation above and Chris's translation below:

_tmp = spam
> if _tmp is not None:
>     _tmp = _tmp.eggs
>     if _tmp is not None:
>         _tmp = _tmp.bacon
> food = _tmp
>

What's a case where they would do something different?

* If spam is None, they work the same -> None
* If spam is not None, but spam.eggs exists and is None, they work the same
-> None
* If spam is not None, but spam.eggs doesn't exist, they work the same ->
AttributeError
* If spam is not None, and spam.eggs is not None, but spam.eggs.bacon is
None, they work the same -> None
* If spam is not None, and spam.eggs is not None, but spam.eggs.bacon
doesn't exist, they work the same -> AttributeError
* If spam is not None, and spam.eggs is not None, and spam.eggs.bacon is
not None, they work the same -> bacon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180726/da0310aa/attachment-0001.html>


More information about the Python-ideas mailing list