
Oct. 21, 2021
8:49 a.m.
Hello, Le 21/10/2021 à 07:59, Steven D'Aprano a écrit :
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.
This is not true as a general rule: the PEP 505 examples with `dict.get()` do catch too much. Also, if `None` is not special, you are free to make up your own `NoValue` sentinel object, which can raise specific exceptions on attribute access, item access, etc: class NoValueError(Exception): pass class NoValueAttributeError(AttributeError, NoValueError): pass and so on… So this particular point seems solvable. Cheers, Baptiste