<div class="gmail_quote">On Mon, Aug 23, 2010 at 1:33 PM, Raymond Hettinger <span dir="ltr"><<a href="mailto:raymond.hettinger@gmail.com">raymond.hettinger@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div style="word-wrap: break-word;"><div><div>I don't have a specific proposal in mind.</div></div></div></blockquote><div><br>That's why I called it scope creep. :-) Trust me, your proposal will not lead to a quick and better replacement for hasattr(). (See several other people's replies.)<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;"><div><div>My main questions are</div><div><br></div>
<div>* Is there anything that hasattr(obj, key) can or should do that</div><div> can't already be done with getattr(obj, key, None)? </div><div> If not, do we really need to change anything?</div></div></div></blockquote>
<div><br>getattr(obj, 'key', None) returns None when obj.key exists and has the value None. The workaround is ugly.<br> <br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div style="word-wrap: break-word;"><div><div></div><div>* Why do people typically use hasattr() instead getattr()? </div><div> Aren't they are really trying to just determine </div><div> whether a key exists somewhere in the MRO?</div>
<div> If so, then doing anything more than that is probably a surprise.</div></div></div></blockquote><div><br>Most users who call hasattr() probably don't even know what MRO means. They call hasattr() because they want to avoid a try/except clause. The reasons they are not calling getattr(obj, key, None) could be many: never heard of it, too obscure (it surely doesn't spell "has the attribute" like hasattr() does to the beginning user), or (probably common) the actual attribute access is in some other piece of code they are about to call but don't control.<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;"><div><div>I know my own uses of hasattr() do not expect any exceptions at all.</div>
<div>It comes up in duck typing support different handling for different </div><div>types of inputs. If others are using it the same way, I think it is</div><div>unlikely that they have unittests to cover the possibility that</div>
<div>hasattr() would ever start raising exceptions.</div></div></div></blockquote><div><br>It already raises *some* exceptions (those derived from BaseException but not Exception). I think that change was a definite non-event.<br>
</div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;"><div><div class="im"><blockquote type="cite"><div>
I can vouch that the reason hasattr() catches too many exceptions is<br>that when I first added it (around 1990, I think :-) I wasn't very<br>attuned yet to the problems it could cause.<br></div></blockquote><div><br>
</div></div><div>Fire-up the time machine?</div><div><br></div><div><br></div><div>Raymond</div><div><br></div></div><div><br></div><div>P.S. The current behavior seems to be deeply embedded:</div></div></blockquote><div>
<br>But note that hasattr() doesn't call those.<br></div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;">
<div><span style="font-family: sans-serif; font-size: 16px;"><dl style="margin-bottom: 15px;"><dt>int <tt style="background-color: transparent; padding: 0px 1px; font-size: 1.2em; font-weight: bold;">PyObject_HasAttr</tt><big>(</big><a title="PyObject" href="http://structures.html#PyObject" style="color: rgb(53, 95, 124); text-decoration: none;" target="_blank">PyObject</a><i> *o</i>, <a title="PyObject" href="http://structures.html#PyObject" style="color: rgb(53, 95, 124); text-decoration: none;" target="_blank">PyObject</a><i> *attr_name</i><big>)</big><a href="#12aa0a7c7d265b64_PyObject_HasAttr" title="Permalink to this definition" style="color: rgb(198, 15, 15); text-decoration: none; font-size: 0.8em; padding: 0px 4px;"></a></dt>
<dd style="margin-top: 3px; margin-bottom: 10px; margin-left: 30px; text-align: justify; line-height: 20px;">Returns <tt style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span>1</span></tt> if <i>o</i> has the attribute <i>attr_name</i>, and <tt style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span>0</span></tt> otherwise. This is equivalent to the Python expression <tt style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span>hasattr(o,</span> <span>attr_name)</span></tt>. This function always succeeds.</dd>
</dl><dl style="margin-bottom: 15px;"><dt>int <tt style="background-color: transparent; padding: 0px 1px; font-size: 1.2em; font-weight: bold;">PyObject_HasAttrString</tt><big>(</big><a title="PyObject" href="http://structures.html#PyObject" style="color: rgb(53, 95, 124); text-decoration: none;" target="_blank">PyObject</a><i> *o</i>, const char<i> *attr_name</i><big>)</big><a href="#12aa0a7c7d265b64_PyObject_HasAttrString" title="Permalink to this definition" style="color: rgb(198, 15, 15); text-decoration: none; font-size: 0.8em; padding: 0px 4px;"></a></dt>
<dd style="margin-top: 3px; margin-bottom: 10px; margin-left: 30px; text-align: justify; line-height: 20px;">Returns <tt style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span>1</span></tt> if <i>o</i> has the attribute <i>attr_name</i>, and <tt style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span>0</span></tt> otherwise. This is equivalent to the Python expression <tt style="background-color: rgb(236, 240, 243); padding: 0px 1px; font-size: 0.95em;"><span>hasattr(o,</span> <span>attr_name)</span></tt>. This function always succeeds.</dd>
</dl></span><div><br></div></div><br></div></blockquote></div><br><br clear="all"><br>-- <br>--Guido van Rossum (<a href="http://python.org/~guido">python.org/~guido</a>)<br>