Python equivalent of static member data?

Mark Pilgrim f8dy at my-deja.com
Sat Feb 10 20:01:39 EST 2001


In article <#USeY36kAHA.359 at cpmsnbbsa07>,
  "Roger H" <rhyde99 at email.msn.com> wrote:
> I've just gotten done digging through the docs for 1.5.2,
> and did not find an answer to my question:  Is there a
> Python equivalent to the C++ concept of static member
> data?  I'd like to make a class, and have each instance
> of that class share a variable and its associated value.
> Each instance would have the ability to modify that value,
> and the new value would be updated through all instances.

In Python, these are called class attributes.  You can define them at design
time in your class by putting the attribute definition inside your class but
outside any methods.  You can also access/add/modify them at run time through
the built-in __class__ attribute in each instance of the class.

This is explained in more detail in my book, with an example of exactly what
you're trying to do here. 
http://diveintopython.org/fileinfo_classattributes.html

-M
--
You're smart; why haven't you learned Python yet?  http://diveintopython.org/


Sent via Deja.com
http://www.deja.com/



More information about the Python-list mailing list