Class Variable Question

phawkins at connact.com phawkins at connact.com
Thu Apr 19 15:42:02 EDT 2001


>>>>> "RJ" == Robert Johnson <rjohnson at exotic-eo.com> writes:

somebody else had said:
>> It's not entirely clear to me whether you mean to be talking about
>> setting attributes of classes or attributes of class instances.

RJ> I am talking about "creating" attributes in class instances (not
RJ> setting).  I come from the C++ community where the definition of a
RJ> class is separate from its implementation.  My ambiguous question
RJ> and the variety of replies I received actually helped me to
RJ> understand that in Python a Class Object is created the moment it
RJ> is written and is separate from the Object instance,

Well, no.  C++ also has class objects, and in both Python and C++, a
class object is created when the code is run (one per definition).
Ever heard of a class member variable or class method in C++?  Those
are members that belong to the class object, not the instance object,
and (in C++ or Python) if you change the value of a class member
variable, it changes for all the class's instance objects.

Similarly, you can call a class member function without instantiating
an object (all constructors are class member functions, for example).

So we are talking about three different things: class definitions,
class objects, and instance objects -- again, in both C++ and Python
(and other languages on which I am not so well prepared to hold
forth!)

The class definition exists in the written or compiled code;  class
objects and instance objects are runtime beasts.

RJ> which explains why some thought it strange that I would want to be
RJ> able to block the adding of attributes, effectively blocking a
RJ> simple form of inheritance.  In C++ the class object IS the
RJ> instance and many C++ programming books use the terms "Object" and
RJ> "Instance" interchangeably.

Well, an instance is one type of object, but a class object is another.  

And you are far, far from alone in your confusion!


-- Patricia
Patricia J. Hawkins
Hawkins Internet Applications, LLC





More information about the Python-list mailing list