attribute assignment effects all class instances

Peter Hansen peter at engcorp.com
Fri Sep 10 03:19:41 CEST 2004


anon wrote:
> I'm aware that you can assign a value to an attribute in all class
> instances by assigning to <Class>.<attribute>, however, my case is
> slightly different and bizarre.

Jeffrey Froman gave you a correct answer already, but I wanted
to point out a problem with the above, which might indicate
another area where you have a misunderstanding.

What you say above is technically wrong, so I hope it was just
a poor choice of wording.  Assigning to Class.attribute as you
describe absolutely does NOT "assign a value to an attribute in
all class instances".  What it actually does is it "assigns a
value to an attribute in the class", and that attribute just
happens to be *visible* to all instances.  They do not actually
have that attribute themselves, however.

I put the "assigns a value" part in quotation marks as well,
because in Python it is more accurate to talk about "binding
names" than assigning values.  Thus you should really say that
you can bind a name in the class and it will be visible to
all instances, but the instances themselves do not have that
name bound...

Hope that helps clarify things, or at least I hope it helps
you in asking subsequent questions. :-)

-Peter



More information about the Python-list mailing list