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

David Mertz mertz at gnosis.cx
Wed Jul 25 22:30:13 EDT 2018


Btw. Here's a way of spelling the proposed syntax that gets the semantics
right:

>>> # pip install coalescing
>>> NullCoalesce(spam).eggs.bacon

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

> So now at least TWO proponents of 505 cannot successfully translate a very
> simple example taken almost directly from the PEP!
>
> Is that REALLY a good argument for it being helpful, and not being a bug
> magnet?!
>
> On Wed, Jul 25, 2018 at 9:57 PM Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Thu, Jul 26, 2018 at 11:45 AM, Nicholas Chammas
>> <nicholas.chammas at gmail.com> wrote:
>> > On Wed, Jul 25, 2018 at 9:20 PM Chris Angelico <rosuav at gmail.com>
>> wrote:
>> >>
>> >> On Thu, Jul 26, 2018 at 11:02 AM, David Mertz <mertz at gnosis.cx> wrote:
>> >> > That is disingenuous, I think.  Can this raise an AttributeError?
>> >> >
>> >> >     spam?.eggs?.bacon
>> >> >
>> >> > Of course it can! And this is exactly the pattern used in many
>> examples
>> >> > in
>> >> > the PEP and the discussion. So the PEP would create a situation where
>> >> > code
>> >> > will raise AttributeError in a slightly—and subtly—different set of
>> >> > circumstances than plain attribute access will.
>> >>
>> >> I don't understand. If it were to raise AttributeError, it would be
>> >> because spam (or spam.eggs) isn't None, but doesn't have an attribute
>> >> eggs (or bacon). Exactly the same as regular attribute access. How is
>> >> it slightly different? Have I missed something?
>> >
>> >
>> > That was my reaction, too.
>> >
>> >     food = spam?.eggs?.bacon
>> >
>> > Can be rewritten as:
>> >
>> >     food = spam
>> >     if spam is not None and spam.eggs is not None:
>> >         food = spam.eggs.bacon
>> >
>> > They both behave identically, no? Maybe I missed the point David was
>> trying
>> > to make.
>>
>> Aside from questions of repeated evaluation/assignment, yes. The broad
>> semantics should be the same.
>>
>> (If you want to get technical, "spam" gets evaluated exactly once,
>> "spam.eggs" a maximum of once, and "food" gets assigned exactly once.
>> Your equivalent may evaluate and assign multiple times.)
>>
>> ChrisA
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> --
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>


-- 
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons.  Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180725/65c45eea/attachment.html>


More information about the Python-ideas mailing list