A TYPICAL NEWBIE MISTAKE?

François Pinard pinard at iro.umontreal.ca
Mon May 15 10:15:48 EDT 2000


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

> My key point was that it's easy to get confused when assignment to
> self.attribute overrides the common class attribute. [...]

The question was surely interesting.  In any case, from my own experience, I
found out that common class attributes (besides the class methods themselves)
are not much used in practice.  There are only three cases where I found
them useful:

* To get speedier __init__ by presetting some slow-to-compute parameters,
  when these are independent of actual __init__ parameters, since the
  computation occurs once when the class gets defined.

* To build overall dictionaries or list of all instances of a class, by
  presetting the dictionary to empty as a common attribute, and having each
  __init__ to register the newly created instance to that dictionary.

* To ease the writing and access of some dictionaries, using classes usually
  having no methods; my favourite is a `run' class which holds all the global
  option values, and nothing else, which I preset with default option values.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard






More information about the Python-list mailing list