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 8547573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:17:42+00:00 2026-06-11T13:17:42+00:00

Consider the following python code: class Foo(object): def __init__(self, value): self._value = value @property

  • 0

Consider the following python code:

class Foo(object):

    def __init__(self, value):
        self._value = value

    @property
    def value(self):
        return "value: {v}".format(v=self._value)

    @value.setter
    def value(self, value):
        self._value = value

class Bar(object):

    def __init__(self):
        self.foo = Foo('foo')

    def __getattr__(self, attr, *args, **kwargs):
        """
        Intercepts attribute calls, and if we don't have it, look at the
        webelement to see if it has the attribute.
        """

        # Check first to see if it looks like a method, if not then just return
        # the attribute the way it is.
        # Note: this has only been tested with variables, and methods.
        if not hasattr(getattr(self.foo, attr), '__call__'):
            return getattr(self.foo, attr)

        def callable(*args, **kwargs):
            '''
            Returns the method from the webelement module if found
            '''
            return getattr(self.foo, attr)(*args, **kwargs)
        return callable

>>> b = Bar()
>>> b.foo
<__main__.Foo object at 0x819410>
>>> b.foo.value
'value: foo'
>>> b.foo.value = '2'
>>> b.foo.value
'value: 2'
>>> b.value
'value: 2'
>>> b.value = '3'
>>> b.value
'3'

That last part, I want it to be ‘value: 3’ instead of ‘3’ because now my property ‘value’ is now an attribute instead.

Is it possible, and if it is how would I would I do that.

  • 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-11T13:17:43+00:00Added an answer on June 11, 2026 at 1:17 pm

    Your __getattr__ returns the property value, not the property itself. When you access getattr(self.foo, attr) it does the equivalent of self.foo.value and returns that, and the property is called at that time.

    You thus need to implement a __setattr__ method too, to mirror the __getattr__ and pass on the value setting to the contained foo object.

    Under the hood, Python implements properties as descriptors; their __get__() method is called by the lower-level __getattribute__ method, which causes them to return their computed value. It is never the property object itself that is returned.

    Here’s an example __setattr__:

    def __setattr__(self, attr, value):
        if hasattr(self, 'foo') and hasattr(self.foo, attr):
            setattr(self.foo, attr, value)
            return
        super(Bar, self).__setattr__(attr, value)
    

    Note: because your __init__ sets self.foo, you need to test if foo exists on your class (hasattr(self, 'foo'). You also need to call the original __setattr__ implementation to make sure that things like self.foo = Foo() work still.

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

Sidebar

Related Questions

consider the following python code class Base(object): def __init__(self): self.foo = 5 class Derived(Base):
Consider the following Python (3.x) code: class Foo(object): def bar(self): pass foo = Foo()
Consider the following code in Python: class A(object): CLASS_ATTRIBUTE = 42 def f(self): return
In Python, consider I have the following code: class SuperClass(object): def __init__(self, x): self.x
consider the following python code: import gtk class MainWindow(): def __init__(self): self.window = gtk.Window()
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Consider the following code: class MyCustomDescriptor: def __init__(self,foo): self._foo = foo def __call__(self,decorated_method): #
Consider the following class: class SquareErrorDistance(object): def __init__(self, dataSample): variance = var(list(dataSample)) if variance
Consider the following example code class A: def __init__(self, i): self.i = i print("Initializing

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.