Static variable vs Class variable

Duncan Booth duncan.booth at invalid.invalid
Wed Oct 17 10:00:20 CEST 2007


paul.melis at gmail.com wrote:

> On Oct 10, 8:23 am, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
<snip>
>> rebinds a. Period. Which is the _essential_ thing in my post, because
>> this rebinding semantics are what confused the OP.
> 
> Doesn't this depend on wether "a" supports __iadd__ or not? Section
> 3.4.7 of the docs say
> 
> """
> If a specific method is not defined, the augmented operation falls
> back to the normal methods. For instance, to evaluate the expression x
> +=y, where x is an instance of a class that has an __iadd__() method,
> x.__iadd__(y) is called. If x is an instance of a class that does not
> define a __iadd__() method, x.__add__(y) and y.__radd__(x) are
> considered, as with the evaluation of x+y.
> """
> 
> So if a.__iadd__ exists, a += b is executed as a.__iadd__(b), in which
> case there's no reason to rebind a.
> 
You misunderstand the documentation, what you quoted doesn't say that 
the assignment is suppressed. If a.__iadd__ exists, a += b is executed 
as a = a.__iadd__(b)

The options for a+=b are:

   a = a.__iadd__(b)
   a = a.__add__(b)
   a = b.__radd__(a)

but the assignment always happens, it is only what gets executed for the 
right hand side which varies.



More information about the Python-list mailing list