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

Antoine Pitrou solipsis at pitrou.net
Wed Oct 16 13:50:46 CEST 2013


Le Wed, 16 Oct 2013 13:42:34 +0200,
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.
> 
> By the way, what are the performances of contextlib.ignore()?
> Exceptions can be slow in some cases. Adding something even slower
> would not be a good idea.

A "try" block which succeeds is fast. A context manager will always be
slower, especially when written in pure Python with a generator wrapped
in a contextlib.contextmanager.

Regards

Antoine.




More information about the Python-Dev mailing list