[Python-ideas] Null coalescing operators

Steven D'Aprano steve at pearwood.info
Tue Sep 22 04:15:46 CEST 2015

On Mon, Sep 21, 2015 at 04:23:03PM -0400, Terry Reedy wrote:

> try:
>     process(re.match(needle, haystack).group())
> except AttributeError:  # no match
>     no_needle()
> is equivalent unless process can also raise AttributeError.

It is difficult to guarantee what exceptions a function will, or won't, 
raise. Even if process() is documented as "only raising X", I wouldn't 
be confident that the above might not disguise a bug in process as "no 

This is why the standard idiom for using re.match is to capture the 
result first, then test for truthiness (a MatchObject), or None-ness, 
before processing it.


More information about the Python-ideas mailing list