
I am rather fond of the idea of null-coalescing, at the very least, for mutable default values: def foo(a=None): a ??= [] ... but I worry about the code messes we will run into with some of the other options. Woe be unto anyone forced to understand the behavior of: thing?.attr?[key]?.subattr ?? 127 What if we added the Elvis operator "?:" for null coalescing and left the rest for future consideration On Wed, Jul 18, 2018 at 10:49 PM Tim Peters <tim.peters@gmail.com> wrote:
[Steve Dower <steve.dower@python.org>]
...
* The "``None``-aware attribute access" operator ``?.`` evaluates the
complete expression if the left hand side evaluates to a value that is not ``None``
And if the LHS does evaluate to `None` ...? I'll assume the result is also `None` then.
...
From ``inspect.py``::
for base in object.__bases__: for name in getattr(base, "__abstractmethods__", ()): value = getattr(object, name, None) if getattr(value, "__isabstractmethod__", False): return True
After updating to use the ``?.`` operator (and deliberately not converting to use ``any()``)::
for base in object.__bases__: for name in base?.__abstractmethods__ ?? (): if object?.name?.__isabstractmethod__: return True
I got lost on the `for` here. The part following `in`:
for name in getattr(base, "__abstractmethods__", ()):
looks in `base` (regardless of whether `base` is `None`) for an attribute named "_abstractmethods__".. If such an attribute exists, the value of the attribute is returned (`None` or not). Else an AttributeError is swallowed and `()` is returned. It's hard to see how
for name in base?.__abstractmethods__ ?? ():
does the same. If `base` itself is `None`, I guess it returns `()`, or if `base` has an "_abstractmethods__" attribute then the value of that attribute is returned - unless its value is None, in which case `()` is again returned. But if `base` is not `None` and the attribute does not exist, doesn't this raise AttributeError? The later "Exception-aware operators" section seemed to explicitly reject the idea that `?.` and `?[]` would suppress AttributeError and/or TypeError.
In short, the original getattr() didn't care at all whether `base` was `None`, or whether the value of its "__abstractmethods__" attribute was `None`, but cared a whole lot about whether that attribute exists. I just can't see how the updated code matches that in any of those respects.
Ignoring that and pressing on, I suffer the same kind of confusions on the `if` part. What am I missing? For example, do these operators swallow exceptions after all?
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/