Python Property
Overview
class dog: def __init__(self): self._name = "Bello" @property def name(self) -> str: print("Setter method called") return self._name @name.setter def name(self, name): if isinstance(name, str): self._name = name print("Getter method called") else: return
snowy = dog() snowy.name = "Snowy" snowy.name
Setter method called Getter method called 'Snowy'
Benefits
- @property: for the caller name becomes an attribute (not a method any more)
- no more use of parentheses when calling a getter:
a = snowy.nameinstead ofa = snowy.name() - the hidden getter still can return a deep copy or converted attribute (bytearray instead of byte, list instead of set, ...)
- no more use of parentheses when calling a getter:
- @name.setter: for the caller name can set by
=- no more use of parentheses when calling a setter:
snowy.name = "Snowy"instead ofsnow.name("Snowy") - the hidden setter still can use type checks or throw errors or do conversions
- no more use of parentheses when calling a setter:
