Style question - defining immutable class data members

John Posner jjposner at
Thu Mar 26 02:36:47 CET 2009

On Mon Mar 16 03:42:42, I said:

> RTFM, in section "Augmented assignment statements" of python301.chm:
> ---
> For targets which are attribute references, the initial value is retrieved

> with a getattr() and the result is assigned with a setattr(). Notice that
> two methods do not necessarily refer to the same variable. When getattr() 
> refers to a class variable, setattr() still writes to an instance
> For example:
> class A:
>     x = 3    # class variable
> a = A()
> a.x += 1     # writes a.x as 4 leaving A.x as 3
> ---
> So, this case is closed ... almost. I believe a similar explanation, with
> similar example, should appear in the preceding/parent section,
> statements". Here's my proposed example:
>    class A:
>        x = 3         # class variable
>    a = A()
>    a.x = a.x + 1     # a.x on RHS gets value of class variable (3)
>                      # a.x on LHS creates instance variable with
>                        value of RHS expression (4)

Following is a draft of my proposed addendum to the "Assignment statements"
section of the Python documentation (for both Python 2 and Python 3). I've
included reStructured Text markup. The first paragraph is what's already in
the documentation. Everything else is mine:

  * If the target is an attribute reference: The primary expression in 
    the reference is evaluated. It should yield an object with 
    assignable attributes; if this is not the case, TypeError is raised. 
    That object is then asked to assign the assigned object to the given 
    attribute; if it cannot perform the assignment, it raises an 
    exception (usually but not necessarily AttributeError).

    If the object is a class instance and the attribute reference occurs
    on both sides of the assignment operator; for example::

        self.x = self.x + 1

    ... in the RHS expression, ``self.x`` is evaluated with 
    ``getattr()``, which can access either an instance attribute or (if 
    no instance attribute exists) a class attribute. The LHS target 
    ``self.x`` is assigned with ``setattr()``, which *always* accesses 
    an instance attribute, creating it if necessary. Thus, the two 
    occurrences of ``self.x`` do not necessarily refer to the same 
    variable. If the RHS expression refers to a class attribute, the LHS 
    creates a new instance attribute as the target of the assignment.

    See section "Augmented assignment statements" for a similar note on 
    attribute references. 

If anyone would like to suggest changes to this write-up, have at it. I plan
to submit a SourceForge issue within a day or so.

(BTW, I searched through the existing issues, to see if it has already been
reported. AFAIK, it hasn't. But #4246 is a cousin, referencing
UnboundLocalError exceptions.


E-mail message checked by Spyware Doctor (
Database version: 5.12040

More information about the Python-list mailing list