"Undeclared variable" declaration in python
<p>The following code is an example:</p>

<pre><code>class A(object):
    def f(self):
        pass
A.f.b = 42
</code></pre>

<p>How is this variable being allocated? If I declare A.f.a, A.f.b, and A.f.c variables am I creating 3 different objects of A? Can someone explain what's going on in memory (as this does not appear to be something easily coded in C)?</p>

<p><code>A.b = 42</code> adds a class variable to <code>A</code>, and thus makes it visible instantly for each instance of <code>A</code> (but only 1 entry in memory)</p>

<p>You can add attributes to classes and instances anytime you like in Python. The cleanest way would be to do it a declare time or this could be misleading.</p>

<pre><code>class A:
    b = 12
</code></pre>

<p>But for quick "extensions" of classes or instances you could choose to dynamically add them.</p>

<p>ex:</p>

<pre><code>class A(object):
    pass

a = A()
print('b' in dir(a))  # False
A.b = 42
print('b' in dir(a))  # True even if instanciated before creation of `b`
</code></pre>

python