On Wed, Jul 25, 2018 at 10:12 PM David Mertz <me...@gnosis.cx> wrote:
> On Wed, Jul 25, 2018 at 9:47 PM Nicholas Chammas < > nicholas.cham...@gmail.com> 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. >>> >> > >> 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. >> > > No, you illustrate it perfectly! I had to stare at your translation for a > while to decide if it was really identical to the proposed > `spam?.eggs?.bacon`. The fact I have to think so hard makes the syntax > feel non-obvious. > > Plus, there's the fact that your best effort at translating the proposed > syntax is WRONG. Even a strong proponent cannot explain the behavior on a > first try. And indeed, it behaves subtly different from plain attribute > access in where it raises AttributeError. > > >>> spam = SimpleNamespace() > >>> spam.eggs = None > >>> spam.eggs.bacon > AttributeError: 'NoneType' object has no attribute 'bacon' > > >>> # spam?.eggs?.bacon > >>> # Should be: None > > >>> "Translation" does something different > >>> food = spam > >>> if spam is not None and spam.eggs is not None: > ... food = spam.eggs.bacon > >>> food > namespace(eggs=None) > 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? :) What misled me was this example from the PEP <https://www.python.org/dev/peps/pep-0505/#the-maybe-dot-and-maybe-subscript-operators> showing how atoms are evaluated. The breakdown begins with `_v = a`, so I copied that pattern incorrectly when trying to explain how an example assignment would work, instead of translating directly from what I understood the PEP 505 variant should do. So, shame on me. I think this particular mistake reflects more on me than on PEP 505, but I see how this kind of mistake reflects badly on the folks advocating for the PEP (or at least, playing devil's advocate).
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/