[Python-Dev] Re: PEP 505 (None-aware operators) for Python 3.11

2021年10月20日 23:10:51 -0700

On Wed, Oct 20, 2021 at 06:17:59PM +0200, Piotr Waszkiewicz wrote:
> Do you think about something along those lines?
> ```
> phone = book.publisher.owner.phone except AttributeError: None
> ```
This is not equivalent to PEP 505's None-aware operators. The semantics 
are very different, and it is much less safe.
If you misspell an attribute:
 book.publisher.onwer = Owner(...)
then the `except AttributeError` code will silently hide that error and 
return None. PEP 505 does not do that.
If you use the wrong type:
 book.publisher = "O'Reilly Books"
then the `except` version will silently hide the error and return None. 
PEP 505 does not.
Versions of this that rely on catching AttributeError are simply wrong 
and are an anti-pattern. They catch too much and silently turn 
errors into silent wrong behaviour.
 
PEP 505 does not fall into that trap.
-- 
Steve
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/B5LUO5K562IM6667YKDW6N57YEUMJKXG/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to