[Python-Dev] cpython: Rename contextlib.ignored() to contextlib.ignore().

R. David Murray rdmurray at bitdance.com
Wed Oct 16 16:36:04 CEST 2013


Victor Stinner <victor.stinner at gmail.com> a écrit :
> I might agree with idea of contextlib.ignore() (I'm still opposed to
> the idea), but I don't understand the purpose of adding a new syntax
> doing exactly the same than try/except:
> 
> >     with trap(OSError) as cm:
> >         os.unlink('missing.txt')
> >     if cm.exc:
> >         do_something()
> 
> Nobody noticed that this can be written:
> 
> try:
>   os.unlink('missing.txt')
> except OSError as err:
>   # do something with err
> 
> ?? What happened with the Zen Principle "There should be one-- and
> preferably only one --obvious way to do it." I don't understand why I
> would import contextlib and use a new context manager, whereas
> try/except is already a builtin feature of Python.

For the record, that was *not* my proposal.  You'd be silly to replace
the try/except with the context manager in the above case.  The case
where the CM would be useful would be if there were statements
in between the context manager and the 'if'.

And that *still* wasn't the proposal.  My proposal was to provide the
exception on the CM so that the name 'trap' made sense, and so that
it was clear that only one exception was ever caught, and so that
the CM was parallel in functionality to the existing assertRaises
CM.

But none of this affects the code, because Nick isn't accepting the
proposal, and that's fine.

--David


More information about the Python-Dev mailing list