Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:34:40+00:00 2026-06-15T09:34:40+00:00

Possible Duplicate: Private functions / Variables enforcement in python Can one create an equivalent

  • 0

Possible Duplicate:
Private functions / Variables enforcement in python

Can one create an equivalent to the following class with two static properties, one read-only and the other read-write in Python?

class Foo
{
    private static string _rdy = "RO";
    public static string rd
    {
        get { return _rd; }
    }

    private static string _rw = "RW";
    public static string rw
    {
        get { return _rw ; }
        set { _rw = value; }
    }
}

I know read-only class properties are possible in Python, but how I have not seen any examples of read-write class properties in Python. Is it possible? Basically I want:

class classproperty(object):

    def __init__(self, getter
            #, setter
        ):
        self.getter = getter
    #    self.setter = setter

    def __get__(self, instance, owner):
        return self.getter(owner)

    # Could there be a __set__ here?
    #vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    #def __set__(self, instance, owner, value):
    #    self.setter(owner, value)
    #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

class Foo(object):

    _ro = "RO"
    _rw = "RW"

    @classproperty
    def ro(cls):
        return cls._ro

    # How would you this?
    #vvvvvvvvvvvvvvvvvvvv
    #@classproperty.setter
    #^^^^^^^^^^^^^^^^^^^^
    #def rw(cls, value):
    #    cls._rw, value

# This is what I need
>>> Foo.ro
RO
>>> Foo.ro = 'X'     # Hypothetical Exception
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: class 'Foo' attribute 'ro' is not writable!
>>> Foo.rw
RW
>>> Foo.rw = 'NEW'
>>> Foo.rw
NEW
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:34:42+00:00Added an answer on June 15, 2026 at 9:34 am

    I think you want to use the property() built-in. http://docs.python.org/2/library/functions.html#property

    While conventionally, it is used on an instance, I think you may be able to use it on a metaclass to achieve what you want.

    class MetaFoo(type):
        _ro = "RO"
        _rw = "RW"
    
        def _get_ro(self):
            return self._ro
        def _get_rw(self):
            return self._rw
        def _set_ro(self, value):
            raise AttributeError("class 'Foo' attribute 'ro' is not writable!")
        def _set_rw(self, value):
            self._rw = value
        ro = property(_get_ro, _set_ro)
        rw = property(_get_rw, _set_rw)
    
    
    class Foo(object):
        __metaclass__=MetaFoo
    
    
    assert Foo.rw == "RW"
    Foo.rw = 'NEW'
    assert Foo.rw == "NEW"
    
    assert Foo.ro == "RO"
    
    try:
        Foo.ro = 'X'
    except Exception as e:
        assert str(e) == "class 'Foo' attribute 'ro' is not writable!", str(e)
        assert type(e) == AttributeError
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can inner classes access private variables? Inner class accessing outer class I
Possible Duplicate: Initializing private static members Why I can't initialize non-const static member or
Possible Duplicate: Making a method private in a python subclass Private Variables and Methods
Possible Duplicate: Can inner classes access private variables? So I'm trying to use a
Possible Duplicate: Does C++ call destructors for global and class static variables? What is
Possible Duplicate: What does void(U::*)(void) mean? Considering the following: template <class T> class myButtoncb
Possible Duplicate: how do i access static member of a class? I have this
Possible Duplicate: Private/protected inheritance Can you give me example for private inheritance in C++
Possible Duplicate: Difference between private, public and protected inheritance in C++ One of the
Possible Duplicate: Cannot access private member in singleton class destructor I'm implementing a singleton

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.