Note @property registers a custom property with a specific type, enabling the browser to animate it. Without registration, custom properties are strings that cannot be transitioned. Supported types include <color>, <length>, <angle>, <number>, <percentage>.
classTemperature:def__init__(self, celsius:float):self._celsius= celsius
@property
deffahrenheit(self)->float:returnself._celsius*9/5+32
@property
defcelsius(self)->float:returnself._celsius
@celsius.setterdefcelsius(self, value:float)->None:if value <-273.15:raiseValueError("Below absolute zero")self._celsius= value
t =Temperature(100)print(t.fahrenheit)
t.celsius=0print(t.fahrenheit)
Output
212.0
32.0
Note @property lets you expose computed values as attributes and add validation to setters without changing the caller's syntax.
Frequently asked questions
How does HTML & CSS handle property?
This task is covered in 2 stacks on this page: HTML & CSS, Python. The "@property (Typed Custom Properties)" snippet in HTML & CSS uses `@property --name {`.
Which code does the HTML & CSS example use?
The "@property (Typed Custom Properties)" snippet uses `@property --name {`, from the Modern CSS section of the HTML & CSS cheat sheet.
Which stacks cover "property" on this page?
HTML & CSS, Python. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "@property (Typed Custom Properties)": @property registers a custom property with a specific type, enabling the browser to animate it. Without registration, custom properties are strings that cannot be transitioned. Supported types include <color>, <length>, <angle>, <number>, <percentage>.