Skip to content

Python:@property

Getter and Setter

class Parrot:
    def __init__(self):
        self._voltage = 100000

    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage

    @voltage.setter
    def voltage(self, value):
        """Set the new voltage."""
        self._voltage = value

getter는 @property를 사용하면 된다. 이렇게 @(property 이름).setter를 지정하면 setter를 만들 수 있다. 원래의 Property와 동일한 이름의 함수를 추가해야 함에 유의하자. (위의 예제에서는 voltage)

See also