0

Suppose the following code is given, Python 2.7

class A:
    def __init__(self, x):
        self.x = x

    @property
    def x(self):
        return self.__x

    @x.setter
    def x(self, x):
        self.__x = x
        self._y = self.x**2

We have an attribute x which once it is set / updated should also automatically set/update the attribute _y. _y is only used for internal calculation in other function. So far the motivation. Now if I do the following

>>> mytest = temp.A(2)
>>> mytest._y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: A instance has no attribute '_y'
>>> 

is it not possible to set multiple attributes within via a property?

math
  • 1,868
  • 4
  • 26
  • 60

0 Answers0