<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:'Courier New', courier, monaco, monospace, sans-serif;font-size:10pt"><div></div><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><div style="font-family:Courier New, courier, monaco, monospace, sans-serif;font-size:10pt"><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><div class="gmail_quote"><div>Is there any compelling reason to write:<br><br>[item.upper() for item in my_list if isinstance(item, basestring)]<br>
<br>rather than the following?<br><br>[item.upper() for item in my_list if hasattr(item, 'upper')]</div></div></div></div></blockquote><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">What happens if you have an object in your list that has </span></font><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">an 'upper' merthod that, say, reboots your PC into an </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">upper level of admin access? Or that sets the bank </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size:
16px;">account credit limit to the upper limit?</span></font><div style="position:fixed"></div></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;"><br></span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">Your list comprehension will happily run and apply upper </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">to the object but the result may not be what you expected.</span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">If you check isinstance you know that you are working
with </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">some kind of string at least. (A programmer could still </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">override upper do do some wacky thing but then that </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">programmer is abusing upper, whereas, in the bank account </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">case it's a perfectly valid application of upper() )</span></font></div><div><font
class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;"><br></span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">Even more to the point the 'upper' that hasattr() finds may </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">not be callable, it could be the upper value of a range for </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">example. Then when you try to use it in the comprehension </span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif"
size="4"><span class="Apple-style-span" style="font-size: 16px;">you will get an error.</span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;"><br></span></font></div><div><font class="Apple-style-span" face="'times new roman', 'new york', times, serif" size="4"><span class="Apple-style-span" style="font-size: 16px;">Alan G.</span></font></div></div></body></html>