Static member variables

Aahz Maruch aahz at netcom.com
Sun Jul 9 14:54:59 EDT 2000


In article <8kahdq$5e2$1 at zingo.tninet.se>,
Thomas Svensson <thomas_s at ebox.tninet.se> wrote:
>
>Is there a way of declaring static (and constant) member variables in
>classes? I have a member variable that should be unique for each class
>type and thus should not waste memory for each instance of the class I
>create. Something like the __doc__ string I guess.

You can have class variables, but they're not exactly what I'd call
"static" and they certainly aren't constant.

class foo:
  bar = 2
  def __init__(self):
    self.baz = 3

f = foo()

f.bar is a class variable; f.baz is an instance variable.  Note that you
can run into problems if you're not careful because of the way scoping
rules work.
--
                      --- Aahz (Copyright 2000 by aahz at netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

"do you want my self-identities alphabetically, chronologically, or in
random order?"  -- Misha



More information about the Python-list mailing list