On Sep 10, 2016 4:45 PM, "Guido van Rossum" <gu...@python.org> wrote: > > There seems to be a major misunderstanding here. A None-coalescing > operator is not for catching AttributeError, it's a shortcut similar > to "a or b" except that it checks for "a is None" rather than bool(a).
That's exactly what the wrapper does. Except it converts all the regular operators into their None-coalescing versions by putting the extra checks into the wrapped object itself. Now admittedly, this DOES mean that the behavior of operations is somewhat different depending on whether they are wrapped objects or not. False-like is a different thing than None-like. This really MUST BE essentially a way a catching AttributeErrors though. With the proposed syntax 'x?.foo?.bar' will resolve even if x has no 'foo'. So 'x?.'foo' has to be something special. I guess that special thing could be a 3.7-style None that responds to new operators, but that's essentially still *wrapping* a 3.6-style None. In my mind, as I say, the question marks look ugly, especially when repeated in chained operations (attribute, call, item get). But even if they didn't feel bad visually, I don't believe the use case is common enough to warrant dedicated syntax. Even if my keyboard had some character I thought was beautiful and intuitive for that meaning, it's still an extra cognitive burden to distinguish the plain from None-coalescing versions of every operation, especially for learners. Another problem is that the question mark still doesn't actually get the special 'a or b' behavior. For that you still need 'a if a is not None else b'. Or I guess, in concept, 'a ?or b'. For what it's worth, the wrapper gives you the special 'a or b' semantics by casting non-Nones as truthy... But again, 'a' has to have been wrapped first. > > On Sat, Sep 10, 2016 at 4:38 PM, David Mertz <me...@gnosis.cx> wrote: > > Sorry, I sent this accidentally as private reply, then tried to fix it on > > phone. The latter produced horrible formatting. Please just read this > > version. > > > > On Sat, Sep 10, 2016 at 4:10 PM, Guido van Rossum <gu...@python.org> wrote: > >> > >> So you're offering `NoneCoalesce(x).bar` as less-ugly alternative to > >> `x?.bar`... Color me unconvinced. > > > > > > No, I'm offering a more realistic use pattern: > > > > for x in get_stuff(): > > > > x = NoneCoalesce(x) > > > > # ... bunch of stuff with x ... > > # ... more stuff with nested keys or attributes ... > > > > x2 = x.foo > > > > x3 = x.bar.baz[x2] > > > > x4 = x(x.val) > > > > result = x3(x4) > > > > > > > > As a less ugly alternative in the fairly uncommon case that you want None > > coalescing as the behavior of getting attributes, keys, call values, etc. > > that may or may not be available (AND where you don't want to wrap all of > > those access patterns in one try/except block). > > > > In contrast, the ugly version of even this pretty simple toy code with the > > hypothetical syntax would be: > > > > for x in get_stuff(): > > > > # ... bunch of stuff with x ... > > > > # ... more stuff with nested keys or attributes ... > > > > x2 = x?.foo > > > > x3 = x?.bar?.baz?[x2] > > > > x4 = x?(x?.val) > > > > result = x3?(x4) > > > > > > This second case looks absolutely awful to me. And real world uses, if > > implemented, would quickly get much worse than that. > > > > Yours, David... > > > > -- > > 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. > > > > _______________________________________________ > > Python-ideas mailing list > > Python-ideas@python.org > > https://mail.python.org/mailman/listinfo/python-ideas > > Code of Conduct: http://python.org/psf/codeofconduct/ > > > > -- > --Guido van Rossum (python.org/~guido)
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/