pythonadvanced
Property Setter
Advanced Python pattern: property-setter
pythonPress ⌘/Ctrl + Shift + C to copy
class Temperature:
def __init__(self, celsius: float):
self.celsius = celsius
@property
def celsius(self) -> float:
return self._celsius
@celsius.setter
def celsius(self, value: float) -> None:
if value < -273.15:
raise ValueError('below absolute zero')
self._celsius = value
t = Temperature(25)
print(t.celsius)Use Cases
- advanced programming
- patterns
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
pythonadvanced
Descriptors and the Property Pattern
Use Python descriptors and @property for controlled attribute access.
Best for: Controlled attribute access
#python#descriptors
pythonbeginner
Type Hints
Advanced Python pattern: type-hints
Best for: advanced programming
#python#advanced
pythonintermediate
Dataclass
Advanced Python pattern: dataclass
Best for: advanced programming
#python#advanced
pythonadvanced
Abc Abstract
Advanced Python pattern: abc-abstract
Best for: advanced programming
#python#advanced