Skip to content
UPEEC

Properties

@property turns a method into an attribute-like getter, with optional setter validation.

Easy
Comments 0
class Resistor:
    def __init__(self, ohms): self.ohms = ohms
    @property
    def ohms(self): return self._ohms
    @ohms.setter
    def ohms(self, v):
        if v <= 0: raise ValueError("must be positive")
        self._ohms = v

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.