How does __setattr__ work with class attributes?
<p>I am trying to understand how exactly __setattr__ works with class attributes. This question came about when I had attempted to override __setattr__ to prevent attributes from being written to in a simple class.</p>

<p>My first attempt used instance level attributes as follows:</p>

<pre><code>class SampleClass(object):

    def __init__(self):
        self.PublicAttribute1 = "attribute"

    def __setattr__(self, key, value):
        raise Exception("Attribute is Read-Only")
</code></pre>

<p>I had originally thought that only external attempts to set the attribute would throw an error, but even the statement inside the __init__ caused the exception to be thrown.</p>

<p>Later I ended up finding another way to do this, while attempting to solve a different problem. What I ended up with was:</p>

<pre><code>class SampleClass(object):
    PublicAttribute1 = "attribute"

    def __setattr__(self, key, value):
        raise Exception("Attribute is Read-Only")
</code></pre>

<p>I expected to get the same result, but to my surpirse I was able to set the class attribute while preventing changes from being made after the initial declaration. </p>

<p>I don't understand why this works though. I know it has to do with class vs. instance variables. My theory is that using __setattr__ on a class variable will create an instance variable of the same name, since I believe I think I have seen this behavior before, but I am not sure.</p>

<p>Can anyone explain exactly what is happening here?</p>

<p><code>__setattr__()</code> applies only to instances of the class. In your second example, when you define <code>PublicAttribute1</code>, you are defining it on the class; there's no instance, so <code>__setattr__()</code> is not called.</p>

<p>N.B. In Python, things you access using the <code>.</code> notation are called attributes, not variables. (In other languages they might be called "member variables" or similar.)</p>

<p>You're correct that the class attribute will be shadowed if you set an attribute of the same name on an instance.  For example:</p>

<pre><code>class C(object):
    attr = 42

 c = C()
 print(c.attr)     # 42
 c.attr = 13
 print(c.attr)     # 13
 print(C.attr)     # 42
</code></pre>

<p>Python resolves attribute access by first looking on the instance, and if there's no attribute of that name on the instance, it looks on the instance's class, then that class's parent(s), and so on until it gets to <code>object</code>, the root object of the Python class hierarchy.</p>

<p>So in the example above, we define <code>attr</code> on the class.  Thus, when we access <code>c.attr</code> (the instance attribute), we get 42, the value of the attribute on the class, because there's no such attribute on the instance. When we set the attribute of the instance, then print <code>c.attr</code> again, we get the value we just set, because there is now an attribute by that name on the instance. But the value <code>42</code> still exists as the attribute of the class, <code>C.attr</code>, as we see by the third <code>print</code>.</p>

<p>The statement to set the instance attribute in your <code>__init__()</code> method is handled by Python like any code to set an attribute on an object. Python does not care whether the code is "inside" or "outside" the class. So, you may wonder, how can you bypass the "protection" of <code>__setattr__()</code> when initializing the object? Simple: you call the <code>__setattr__()</code> method of a class that doesn't have that protection, usually your parent class's method, and pass it your instance.</p>

<p>So instead of writing:</p>

<pre><code>self.PublicAttribute1 = "attribute"
</code></pre>

<p>You have to write:</p>

<pre><code> object.__setattr__(self, "PublicAttribute1", "attribute")
</code></pre>

<p>Since attributes are stored in the instance's attribute dictionary, named <code>__dict__</code>, you can also get around your <code>__setattr__</code> by writing directly to that:</p>

<pre><code> self.__dict__["PublicAttribute1"] = "attribute"
</code></pre>

<p>Either syntax is ugly and verbose, but the relative ease with which you can subvert the protection you're trying to add (after all, if you can do that, so can anyone else) might lead you to the conclusion that Python doesn't have very good support for protected attributes. In fact it doesn't, and this is by design. "We're all consenting adults here." You should not think in terms of public or private attributes with Python. All attributes are public. There is a <em>convention</em> of naming "private" attributes with a single leading underscore; this warns whoever is using your object that they're messing with an implementation detail of some sort, but they can still do it if they need to and are willing to accept the risks.</p>

python