class variable won't icrement!

Jp Calderone exarkun at meson.dyndns.org
Thu Sep 12 15:14:22 CEST 2002


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
> 
> Roman
> 


  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).

 Jp

-- 
It is practically impossible to teach good programming style to
students that have had prior exposure to BASIC: as potential
programmers they are mentally mutilated beyond hope of
regeneration.        -- Dijkstra
--
 9:00am up 114 days, 9:53, 4 users, load average: 0.02, 0.01, 0.00
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 248 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20020912/e970aba5/attachment.pgp>


More information about the Python-list mailing list