i don't really understand member variables..

Mark McEahern marklists at mceahern.com
Wed Oct 23 20:26:34 EDT 2002


[gabor]
> until now i thought i understand member variables...

[snip example]

You need to distinguish between class and instance variables.  All instances
of a class share the same class variables; e.g.,

>>> class foo:
...     count = 0
...     def __init__(self):
...             foo.count += 1
...             self._count = 0
...     def inc(self):
...             self._count += 1
...
>>> foo.count
0
>>> f = foo()
>>> foo.count
1
>>> g = foo()
>>> foo.count
2
>>> f.inc()
>>> f.inc()
>>> f._count
2
>>> g._count
0
>>>

etc.

// m

-





More information about the Python-list mailing list