Actually I can not figure out the setter. Those. With this code, if I initialize an instance of the wall_e class with a value of -100, then it should have a value of 0. For books, in practice, it doesn’t happen anywhere. Can anyone come across

class Robot: def __init__(self, power): self._power = power power = property() @power.setter def power(self, value): if value < 0: self._power = 0 else: self._power = value @power.getter def power(self): return self._power wall_e = Robot(-100) #wall_e.power = 20 print(wall_e.power) 
  • four
    It should not, because during initialization you don’t use property and put self._power around it - andreymal pm
  • @andreymal is it possible in more detail? - ksantd

1 answer 1

 def __init__(self, power): self.power = power 
  • Have you tried what you advise? - ksantd
  • @ksantd ideone.com/6cmOSg - Sergey Gornostaev
  • Thank you so much for a good example. - ksantd