<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jul 10, 2013 at 3:57 PM, David Ripton <span dir="ltr"><<a href="mailto:dripton@redhat.com" target="_blank">dripton@redhat.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class="im">On 07/10/2013 02:01 PM, Nachi Ueno wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
HI folks<br>
<br>
I would like to ask the review criteria in the community.<br>
<br>
Should we use exception for non-exceptional cases when we can use<br>
parameter checking?<br>
<br>
Example1: Default value for array index<br>
<br>
try:<br>
value = list[5]<br>
except IndexError:<br>
value = 'default_value'<br>
<br>
This can be also written as,<br>
<br>
list_a[3] if len(list_a) > 3 else 'default_value'<br>
</blockquote>
<br></div>
Both of these are fine. Neither deserves to be banned.<br>
<br>
But LBYL is often naive in the face of concurrency. Just because something was true a microsecond ago doesn't mean it's still true. Exceptions are often more robust.</blockquote><div><br></div><div style>getattr() takes a default and, as it is implemented in C, is thread-safe. So: <br>
</div><div style><br></div><div style> value = getattr(my_obj, 'might_not_be_there', 'default')</div><div style><br></div><div style>Of course, it's probably better to make sure you've always got the same type of object in the first place but sometimes the attributes change across versions of libraries.</div>
<div style><br></div><div style>For accessing elements of a sequence that may be too short, itertools.chain() and itertools.islice() are useful.</div><div style><br></div><div style><div><div>>>> import itertools</div>
<div>>>> vals1 = ['a', 'b']</div><div>>>> a, b, c = itertools.islice(itertools.chain(vals1, ['c']), 3)<br></div><div>>>> a, b, c</div><div>('a', 'b', 'c')</div>
<div>>>> vals2 = ['a', 'b', 'd']</div><div>>>> a, b, c = itertools.islice(itertools.chain(vals2, ['c']), 3)</div><div>>>> a, b, c</div><div>('a', 'b', 'd')</div>
</div><div><br></div><div style>Doug</div><div style><br></div></div></div></div></div>