472,992 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 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 9170
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Koda | last post by:
What is the difference between class fields and class properties? Thanks Mike
3
by: Jae | last post by:
Hello, here's what I want to do but I'm not sure if it will work because Im not sure how DB2 works with the JVM. Heres my setup: DB2---->UDF Class (lets call it A)----->Static Class (Lets call...
15
by: ad | last post by:
We can define a static member in a class. But can we define a static class, so that all members are static?
7
by: Dave | last post by:
I was just wondering what the difference between using readonly properties in a class definition instead of using a function? Example: Public Class myClass Private privateValues() as Long '...
11
by: Raja Chandrasekaran | last post by:
Hai folks, I have a question to get exact answer from you people. My question is How Static class is differ from instance class and If you use static class in ASP.NET, ll it affect speed or...
3
by: Michael B. Trausch | last post by:
Hello everyone, I am having a problem with static class properties that I suspect is somewhat trivial, but I don't understand what is wrong. I am using GNU C++ on Windows, though I am (hoping)...
2
by: titan.nyquist | last post by:
I need to initialize data to be stored in a static class (for all my code to see), but I'm lost on how to do so. If I use the class constructor, what if it fails? I am reading it information...
2
by: peridian | last post by:
Hello, I should have made a note of which version of php 5 I am currently using, but I didn't (I'm pretty sure it's 5.2.2). Ayway, I just want to clarify that my understanding of the static...
7
by: wagger | last post by:
Hi! I am creating a set of static classes, one class for each "case" I have. The class should do and perform certain things, and the different static classes are doing different things. However,...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.