idiom for initialising variables
Erik Max Francis
max at alcyone.com
Wed Nov 6 15:28:16 EST 2002
Trent Mick wrote:
> Despite having a response that looks almost exactly like mine, Erik's
> response reveals a difference that I didn't really identify: that 'a'
> in
> the former case ("TopLevel" in my example) is shared by all instances.
...
> not-quite-as-swift-as-erik-ly yours,
:-)
There's one significant point that neither of us mentioned, which is
that even when there is a class attribute present, instances _can_
override it locally:
>>> class C:
... s = 1 # this is the class attribute
... def __init__(self, x):
... self.x = x # this is the instance attribute
...
>>> c1 = C(1)
>>> c2 = C(2)
>>> C.s = 10 # this changes it for all instances
>>> c1.s
10
>>> c2.s
10
>>> c1.s = 20 # but this only changes it for the instance c1
>>> c1.s
20
>>> c2.s
10
>>> C.s
10
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ We are victims of our circumstance.
\__/ Sade Adu
Crank Dot Net / http://www.crank.net/
Cranks, crackpots, kooks, & loons on the Net.
More information about the Python-list
mailing list