How to set attributes using property decorators?


Python Problem Overview

This code returns an error: AttributeError: can't set attribute This is really a pity because I would like to use properties instead of calling the methods. Does anyone know why this simple example is not working?


class Bar( object ):
    def value():
      def fget( self ):
          return self._value
      def fset(self, value ):
          self._value = value
class Foo( object ):
    def __init__( self ): = Bar() = "yyy"
if __name__ == '__main__':
    foo = Foo()

Python Solutions

Solution 1 - Python

Is this what you want?

class C(object):
    def __init__(self):
        self._x = None

    def x(self):
        """I'm the 'x' property."""
        return self._x

    def x(self, value):
        self._x = value

Taken from


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohan GulatiView Question on Stackoverflow
Solution 1 - PythonBastien LéonardView Answer on Stackoverflow