471,319 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Static class properties (read-only)

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 = s

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. I know
that using get/set methods, I can make a read-only property at the
object/instance level. Can this be done at the class level? Thanks,

--
Greg

Jul 18 '05 #1
3 9081
In article <vj************@corp.supernews.com>,
Greg Brunet <gr********@NOSPAMsempersoft.com> wrote:

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 = s

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. I know
that using get/set methods, I can make a read-only property at the
object/instance level. Can this be done at the class level? Thanks,


Nope. Gotta do a metaclass. <evil grin>
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

This is Python. We don't care much about theory, except where it intersects
with useful practice. --Aahz
Jul 18 '05 #2

"Greg Brunet" <gr********@NOSPAMsempersoft.com> wrote in message
news:vj************@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

Jul 18 '05 #3
Greg Brunet wrote:
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 = s

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. I know
that using get/set methods, I can make a read-only property at the
object/instance level. Can this be done at the class level? Thanks,


Yes, you can make a class have a read-only property, but you can do this
only by using a custom metaclass. Once you do make it read-only, of
course, nobody can set it, including methods such as __init__ & __del__!-)
Alex

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Koda | last post: by
15 posts views Thread by ad | last post: by
7 posts views Thread by Dave | last post: by
11 posts views Thread by Raja Chandrasekaran | last post: by
3 posts views Thread by Michael B. Trausch | last post: by
2 posts views Thread by peridian | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.