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

2013年10月12日 18:52:35 -0700

On Oct 12, 2013, at 4:30 PM, Ethan Furman <[email protected]> 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
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to