Static class properties (read-only)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Brunet

    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

  • Aahz

    #2
    Re: Static class properties (read-only)

    In article <vj4v20o2qfi7c9 @corp.supernews .com>,
    Greg Brunet <gregbrunet@NOS PAMsempersoft.c om> wrote:[color=blue]
    >
    >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,[/color]

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

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

    Comment

    • Terry Reedy

      #3
      Re: Static class properties (read-only)


      "Greg Brunet" <gregbrunet@NOS PAMsempersoft.c om> wrote in message
      news:vj4v20o2qf [email protected] news.com...[color=blue]
      > In puzzling over classes, I'm wondering if classes can have[/color]
      read-only[color=blue]
      > 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[/color]
      have[color=blue]
      > been created. However, someone could set the value directly.[/color]

      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



      Comment

      • Alex Martelli

        #4
        Re: Static class properties (read-only)

        Greg Brunet wrote:
        [color=blue]
        > 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,[/color]

        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

        Comment

        Working...