Index: reference/simple_stmts.rst =================================================================== --- reference/simple_stmts.rst (revision 74452) +++ reference/simple_stmts.rst (working copy) @@ -156,6 +156,27 @@ assignment, it raises an exception (usually but not necessarily :exc:`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 + :meth:`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 :meth:`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. + (This description does not necessarily + apply to attributes defined with :meth:`property`, which are accessed + with user-defined functions instead of + :meth:`getattr` and :meth:`setattr`). + + See section :ref:`augassign` for a similar note on + attribute references. + .. index:: pair: subscription; assignment object: mutable @@ -253,7 +274,7 @@ *in-place* behavior, the binary operation performed by augmented assignment is the same as the normal binary operations. -For targets which are attribute references, the initial value is retrieved with +If the target is an attribute reference, the initial value is retrieved with a :meth:`getattr` and the result is assigned with a :meth:`setattr`. Notice that the two methods do not necessarily refer to the same variable. When :meth:`getattr` refers to a class variable, :meth:`setattr` still writes to an @@ -264,7 +285,10 @@ a = A() a.x += 1 # writes a.x as 4 leaving A.x as 3 +See section :ref:`assignment` for a similar note on +attribute references. + .. _assert: The :keyword:`assert` statement