[Steve Dower <steve.do...@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/

Reply via email to