Changing __getattr__ during instantiation
<p>I am wanting to change <code>__getattr__</code> during instantiation of the class.  For example:</p>

<pre><code>class AttrTest(object):
    def __init__(self):
        self.__getattr__ = self._getattr

    def _getattr(self, attr):
        print("Getting {}".format(attr))
</code></pre>

<p>I would have expected this to behave like:</p>

<pre><code>class AttrTest(object):
    def __getattr__(self, attr):
        print("Getting {}".format(attr))
</code></pre>

<p>But it does not.</p>

<p>For example, when I run:</p>

<pre><code>&gt;&gt;&gt; at = AttrTest()
&gt;&gt;&gt; at.test
</code></pre>

<p>I would expect both classes to print <code>Getting test</code>, but the top class throws an <code>AttributeError</code>.</p>

<p>Can <code>__getattr__</code> not be changed in this way?</p>

<p>For special methods like <code>__getattr__</code>, Python searches in the base(s) <code>__dict__</code>, not in the instance <code>__dict__</code>.</p>

<p>You can read more details about this in the <a href="https://docs.python.org/3/reference/datamodel.html#special-lookup" rel="nofollow">special lookup</a> section of the data model documentation.</p>

<blockquote>
  <p>I have two implementations of <code>__getattr__</code> that each use a different
  type of serialization (json, pickle), and I wanted my class to be able
  to select one based on a kwarg.</p>
</blockquote>

<p>This is not a good use-case for overriding <code>__getattr__</code>.  Abandon this idea, and instead consider to use <a href="https://docs.python.org/3/library/functions.html#property" rel="nofollow"><code>@property</code></a> or <a href="https://docs.python.org/3/reference/datamodel.html#descriptors" rel="nofollow">descriptors</a> to handle your serialisation dynamically.  </p>

python