John Posner wrote: > 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 > the >> two methods do not necessarily refer to the same variable. When getattr() >> refers to a class variable, setattr() still writes to an instance > variable. >> 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 > a >> similar example, should appear in the preceding/parent section, > "Assignment >> 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
Is this true in the case of read-write properties? This seems a little simplistic for what's actually a pretty complex piece of logic. > 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. > regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Want to know? Come to PyCon - soon! http://us.pycon.org/ -- http://mail.python.org/mailman/listinfo/python-list