Bug or feature?

Gregory A. Landrum landrum at foreman.ac.rwth-aachen.de
Fri May 14 09:44:33 EDT 1999


On Fri, 14 May 1999, Fredrik Lundh wrote:
>
> just pretend that you simply cannot define variables in the
> class namespace!  if you initialize your instance variables in
> the __init__ method (like everyone else already does), things
> will work exactly as you expect.  

That's an easy enough solution, and it's what I started doing
yesterday after figuring out where the problem was.

> could anyone tell me which beginner's document that says
> that Python has class variables, and that using them is a good
> idea?  especially for mutable data types?

I saw the use of class variables in the tutorial (in the section
introducing classes).  It doesn't say that it's a good idea, nor 
mention anything about mutable data types, but nor does it explicitly have
any warnings about not using them.

> given the number of recent posts on this topic, I'm beginning
> to suspect that we have a SERIOUS documentation problem
> here...

Could be. I thought I read through the tutorial fairly carefully, and I
don't remember seeing any mention of the problems I was having.  It's
certainly possible I missed something.

> maybe some of the recent posters could shed some light on
> this: where did you learn about class variables in the first
> place?  and if you used them in an earlier life, what made
> you start using them in Python?

I was stupidly assuming that a variable initialized in the class scope
would act the same way as a variable initialized within the __init__
method.  So I figured I could initialize things which are going to have
default values outside of the __init__ method and thus (I thought) make
the code a little bit easier to read.  I'm still not totally clear on why
this isn't the case, but I can live with knowing that "it is true."

The best solution I've come up with for my own purposes (and the current
#1 entry on my to-do list) is to add an __initvars__ method (which gets
called from __init__) to all of my classes .

Thanks for the answer,
-greg

-- 
---------------------
Dr. Greg Landrum  (landrumSPAM at foreman.ac.rwth-aachen.de)
Institute of Inorganic Chemistry
Aachen University of Technology
Prof.-Pirlet-Str. 1, D-52074 Aachen, Germany






More information about the Python-list mailing list