<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Feb 20, 2014 at 5:45 AM, M.-A. Lemburg <span dir="ltr"><<a href="mailto:mal@egenix.com" target="_blank">mal@egenix.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="">x = data[1] except IndexError return None # is readable<br></div>
f = open('x.txt', 'r') except IOError return None # is probably not a good idea<br>
os.remove('/') except IOError return None # is really bad style<br></blockquote><div><br></div><div>I agree with the comments, but I think the middle one doesn't present the usage that really *would* be useful:</div>
<div><br></div><div> # Fallback to other file-like object</div><div> f = open('x.txt') except IOError return io.StringIO('')</div><div> g = open('y.txt') except IOError return urlopen('<a href="http://the.remote/y.txt">http://the.remote/y.txt</a>')</div>
<div><br></div><div> # Sometimes we want the content of files</div><div> txt = open('x.txt').read() except IOError return ''</div><div><br></div><div>I completely agree that something one calls entirely for its side effect, not its value, is a terrible use for except expressions.</div>
</div><div><br></div>-- <br>Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>
to the 21st century what the slave trade was to the 16th.<br>
</div></div>