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

  • SEARCH
  • Home
  • 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 9111531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:31:43+00:00 2026-06-17T03:31:43+00:00

Is there a way to make a Python @property act as a setter and

  • 0

Is there a way to make a Python @property act as a setter and getter all at once?

I feel like I’ve seen this somewhere before but can’t remember and can’t recreate the solution myself.

For example, instead of:

class A(object):
  def __init__(self, b): self.b = b
  def get_c(self): return self.b.c
  def set_c(self, value): self.b.c = value
  c = property(get_c, set_c)

we could somehow signal that for A objects, the c attribute is really equivalent to b.c for getter, setter (and deleter if we like).

Motivation:

This would be particularly useful when we need A to be a proxy wrapper around B objects (of which b is an instance) but share only the data attributes and no methods. Properties such as these would allow the A and B objects’ data to stay completely in sync while both are used by the same code.

  • 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-17T03:31:44+00:00Added an answer on June 17, 2026 at 3:31 am

    I think you are looking for this forwardTo class as posted on ActiveState.

    This recipe lets you transparently forward attribute access to another
    object in your class. This way, you can expose functionality from some
    member of your class instance directly, e.g. foo.baz() instead of
    foo.bar.baz().

    class forwardTo(object):
        """
        A descriptor based recipe that makes it possible to write shorthands
        that forward attribute access from one object onto another.
    
        >>> class C(object):
        ...     def __init__(self):
        ...         class CC(object):
        ...             def xx(self, extra):
        ...                 return 100 + extra
        ...             foo = 42
        ...         self.cc = CC()
        ...
        ...     localcc = forwardTo('cc', 'xx')
        ...     localfoo = forwardTo('cc', 'foo')
        ...
        >>> print C().localcc(10)
        110
        >>> print C().localfoo
        42
    
        Arguments: objectName - name of the attribute containing the second object.
                   attrName - name of the attribute in the second object.
        Returns:   An object that will forward any calls as described above.
        """
        def __init__(self, objectName, attrName):
            self.objectName = objectName
            self.attrName = attrName
        def __get__(self, instance, owner=None):
            return getattr(getattr(instance, self.objectName), self.attrName)
        def __set__(self, instance, value):
            setattr(getattr(instance, self.objectName), self.attrName, value)
        def __delete__(self, instance):
            delattr(getattr(instance, self.objectName), self.attrName)
    

    For a more robust code, you may want to consider replacing getattr(instance, self.objectName) with operator.attrgetter(self.objectName)(instance). This would allow objectName to be a dotted name (e.g., so you could have A.c be a proxy for A.x.y.z.d).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to make a variable non-inheritable in python? Like in the
Is there any way to make a python program start an interactive debugger, like
What is the simplest way to define setter and getter in Python? Is there
Is there a way to make each form, (all controls, all strings, all integers
Is there some way to make boost::python control the Python GIL for every interaction
Is there a way to make Python logging using the logging module automatically output
is there any elegant way to make Python JSON encoder support datetime? some 3rd
Is there a way to make a list comprehension in Python that only contains
Is there a way to make Python's optparse print the default value of an
Is there a way to make python pickle ignore it's not the same object

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.