Static member variables
Johannes Zellner
johannes at zellner.org
Sun Jul 9 15:07:00 EDT 2000
On Sun, 9 Jul 2000, Thomas Svensson wrote:
> Hello,
>
> 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.
const is not possible, it's up to you not to modify your variables.
static is possible see this example. The class variable (static in
C++ jargon) is named with the class name: `Fred.count'
class Fred:
count = 0
def __init__(self):
Fred.count = Fred.count + 1
def instances(self):
return Fred.count
x = Fred()
print x.instances()
y = Fred()
print x.instances()
z = Fred()
print x.instances()
--
Johannes
More information about the Python-list
mailing list