Carel Fellinger cfelling at iae.nl
Mon May 15 16:13:57 EDT 2000

Courageous <jkraska1 at san.rr.com> wrote:

> This has been an interesting experiment. While I
> was aware that class variables in Python were "static"
> until assigned, it took me until today to learn a bit
> of a stinger of a lesson. To wit: append()ing to
> a list or assigning to a dictionary does not count
> as "assigned".

actually class variables remain 'static' even after assignment!

And as ever you can refer to them as Class.variable. The tricky point
is that you can also refer to them as instance.variable as long
as there is no instance variable yet with that name. Assignment
to instance.variable creates an instance variable with that name,
so hence forth the class variable is only accessible as Class.variable.
Oh, and self is just an other name for the current instance:)

> I spent quite a while this afternoon figuring this
> out. This must be a fairly common error. The solution
> is obvious. If you want to assign class variables as
> commentary the way I do, make sure you assign everyone
> in the __init__() method, ala:

or keep refering to them as class variables (Class.variable)
and not as instance variables (instance.variable), like:

>>> class MyClass
...	i = 'class variable init'
...	def __init__(self):
...		MyClass.i = 'class variable assignment' 
...		self.i = 'instance variable assignment'
>>> instance = MyClass
>>> print `MyClass.i`, `instance.i`
'class variable assignment' 'instance variable assignment'
groetjes, carel

More information about the Python-list mailing list