class variable won't icrement!

Thomas Wouters thomas at xs4all.net
Thu Sep 12 15:44:25 CEST 2002


On Thu, Sep 12, 2002 at 09:14:22AM -0400, Jp Calderone wrote:
> On Thu, Sep 12, 2002 at 12:18:32PM +0200, Roman Yakovenko wrote:
> > Hi it is simple just change 
> > self.id = self.id + 1 
> > To 
> > test.id += 1

>   Actually, that turns out not to be the case.  += behaves differently
> depending on its arguments - sometimes rebinding the name, sometimes
> mutating in place.  For integers, it can not mutate in place (ints are
> immutable, of course!) so "self.id = self.id + 1" is exactly the same as
> "self.id += 1".  Were 'id' a mutable type, say a list, then the above
> solution would work (assuming you changed 1 to something iterable).

Notice how Ramon said 'test.id', not 'self.id. He's changing the class
attribute directly, not through the instance. The switch from
addition+assignment to augmented assignment is just a smoke screen :)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list