[Python-ideas] except expression

Rob Cliffe rob.cliffe at btinternet.com
Thu Feb 20 03:49:14 CET 2014


On 20/02/2014 01:09, Nick Coghlan wrote:
> On 20 February 2014 10:15, Ethan Furman <ethan at stoneleaf.us> wrote:
>> try:
>>     os.unlink(some_file)
>> except OSError:
>>     pass
> Note that Python 3.4+ (and previous versions once I get around to
> doing a new contextlib2 release) allows the statement case to be
> written as the one-liner:
>
>      with suppress(OSError): os.unlink(some_file)
>
> I don't think this PEP needs to worry about that case, but Chris may
> want to explicitly reference contextlib.suppress as being preferred to
> "os.unlink(some_file) except OSError -> None" (or 0 or ... or whatever
> other dummy placeholder someone decided to use).
>
> Cheers,
> Nick.
>
Can I put in another plug for allowing:

     os.unlink(some_file) except OSError: pass

Cons:
     It is anomalous.  It only makes sense in an expression whose value 
is not used.

Pros:
     It is very readable.
     It is a common use case.

But maybe it should go, if anywhere, in a separate PEP.

Thanks,
Rob Cliffe


More information about the Python-ideas mailing list