Why don't objects have hasattr and getattr as attributes?
<p>When you need to access an object's attributes dynamically in Python, you can just use the builtin functions <code>hasattr(object, attribute)</code> or <code>getattr(object, attribute)</code>.</p>

<p>However this seems like an odd order for the syntax to take. It's less readable and intuitive as it messes up the regular sentence structure for English.</p>

<blockquote>
  <p><code>if hasattr(person, age):</code><br>
  if has attribute Person age</p>
</blockquote>

<p>Where having it as a method of the object would be much more readable:</p>

<blockquote>
  <p><code>if person.hasattr(age):</code><br>
  if Person has attribute age</p>
</blockquote>

<p>Is there a particular reason for not implementing it this way? I could imagine there are cases where you're not sure if the object is even a proper object, rather than just <code>None</code>, but surely in those cases of uncertainty you could just use the builtin function anyway for extra safety.</p>

<p>Is there some other drawback or consideration I'm not thinking of that makes adding these not worth it?</p>

<p>Its part of the language design. I guess your find some docs about the more complicated thoughts behind it, but the key points are like</p>

<ul>
<li>You suggest to use a function of an object for a builtin function on all objects. Why should this function be specific to this object?</li>
<li>Semantics: the <code>getattr</code> function works on objects, not as part of an object.</li>
<li>Namespace: The functions of an object are defined by you, not by the language. Internal functions are of the form <code>__getattr__</code> and you will find this function on your object ;-). And getattr uses it internally, so you can even override it (if you know, what you're doing).</li>
</ul>

python class