Static class properties (read-only)

Terry Reedy tjreedy at udel.edu
Thu Aug 7 20:03:05 CEST 2003


"Greg Brunet" <gregbrunet at NOSPAMsempersoft.com> wrote in message
news:vj4v20o2qfi7c9 at corp.supernews.com...
> In puzzling over classes, I'm wondering if classes can have
read-only
> static properties?  I certainly seem to be able to do create static
> properties like this:
>
> class C(object):
>     count = 0
>
>     def __init__(self,s):
>         C.count += 1
>         self.Name = sCC
>
>     def __del__(self):
>         C.count -= 1
>
> and C.count should have a count of the number of its instances that
have
> been created.  However, someone could set the value directly.

Prefixing count with a single underscore will tell others that is is
private-- they should not write it and should not depend on being able
to read it in future versions.  Prefixing with 2 underscores will
invoke name-mangling, which you need to read about before using.

TJR







More information about the Python-list mailing list