Static class properties (read-only)

Discussion in 'Python' started by Greg Brunet, Aug 7, 2003.

  1. Greg Brunet

    Greg Brunet Guest

    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
     
    Greg Brunet, Aug 7, 2003
    #1
    1. Advertising

  2. Greg Brunet

    Aahz Guest

    In article <>,
    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,


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

    This is Python. We don't care much about theory, except where it intersects
    with useful practice. --Aahz
     
    Aahz, Aug 7, 2003
    #2
    1. Advertising

  3. Greg Brunet

    Terry Reedy Guest

    "Greg Brunet" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Aug 7, 2003
    #3
  4. 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
     
    Alex Martelli, Aug 7, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SaravanaKumar
    Replies:
    6
    Views:
    9,514
    Tony Morris
    Oct 19, 2004
  2. keithb
    Replies:
    2
    Views:
    8,068
    keithb
    Jun 7, 2006
  3. George Sakkis

    Read-only class properties

    George Sakkis, Jul 10, 2005, in forum: Python
    Replies:
    3
    Views:
    472
    Bengt Richter
    Jul 11, 2005
  4. bugbear
    Replies:
    4
    Views:
    466
    bugbear
    Aug 23, 2006
  5. A
    Replies:
    1
    Views:
    331
    Ian Collins
    Nov 3, 2010
Loading...

Share This Page