Bug or feature?

Thomas A. Bryan tbryan at arlut.utexas.edu
Sat May 15 09:14:29 EDT 1999


Tim Peters wrote:

> One possibly overused trick in some Python code is to combine the two, like
> so:

[example of a class using class data for instance defaults]

> Create a million of these guys x, and all of them will have x.xcoord == 0,
> but without consuming storage for a million "xcoord" keys in a million
> instance dicts -- they all share the single xcoord/0 key/value pair in the
> *class* dict.  OTOH, an explicit assignment to self.xcoord within a method
> will create a non-shared attr for self, allowing that particular self to
> break free of the class's default xcoord value at the cost of consuming more
> storage.

I've seen that idiom several times, but I never thought about why the 
author was using it.  I'll have to add that to my bag-of-tricks.

Newsgroups are the most frequent cause of that 
 ah-ha-...-now-I-get-it feeling in my life.  Thanks, Tim.

---Tom




More information about the Python-list mailing list