Skip to content
Search

Python:@property

lastmod 2021-08-15created 2021-08-153 edits

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

For a getter, use @property. Specifying @(property 이름).setter like this creates a setter. Note that you must add a function with the same name as the original property (voltage in the example above).

See also

author Zer0 sitemap