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

Raymond Hettinger raymond.hettinger at gmail.com
Sun Oct 13 03:50:51 CEST 2013


On Oct 12, 2013, at 4:30 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

>> 
>> When you ask someone to describe what
>> "try: f() except SomeException: pass" does,
>> they will say that it ignores the exception.
> 
> And they would be right in that case.
> 
> 
>> FWIW, I presented this to 2500+ people in the keynote
>> at the 2013 U.S. Pycon and have received favorable feedback.
> 
> Were you only displaying the same short form above?

Yes.   The specific example given was:

    with ignore(OSError):
         os.remove(somefile)

The intended use for the context manager is for the
common cases like these:

	try:
            os.mkdir(dirname)
        except OSError:
            pass

    def discard(self, key):
         """If the keyed message exists, remove it."""
        try:
            self.remove(key)
        except KeyError:
            pass

Most cases of try/except/pass that I see span only one or two lines
in the try-block.

>  Or did you show some with many lines of code inside the with block?  If you didn't, is that because it's a bad idea?
> 
> Compare:
> 
>    try:
>        f()
>        g()
>        h()
>    except SomeException:
>        pass
> 


Yes, that is usually a bad idea.  
We don't recommend code like that with try/except.
Using a context manager in this case wouldn't make it better.

> For the record, I am no longer opposed to this context manager, only to its name.

It just like the old days where there were 100+ emails suggesting
other names for enumerate() before agreeing that it had been right
to begin with.


Raymond
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20131012/c69b4750/attachment-0001.html>


More information about the Python-Dev mailing list