python getter and setter properties

40 views Asked by At

I am new to properties in Python and a bit confused on how below code is working

class FavoriteShape:
    def __init__(self, color, shape):
        self.color = color
        self.shape = shape

    @property
    def shape(self):
        print('getter method for shape')
        return self.something_else

    @shape.setter
    def shape(self, x):
        print('setter method for shape')
        self.something_else = x

obj1 = FavoriteShape('blue','square')
print('color = ' + obj1.color)
print('shape = ' + obj1.shape)

I understand that in the __init__ function, python calls the setter in the self.shape assignment. But the setter itself is assigning a value , x in this case, to a variable called something_else. How is the value of this something_else is getting assigned to the self.shape ?

I tried googling similar questions and played with Python for some time. I would appreciate anyone's help here. Thanks a lot !!

0

There are 0 answers