Property Decorator in Python
Level 4151 keystrokes
This uses @property to create a computed property, printing 78.5. Properties let you access methods like attributes.
Python code
# Define a class representing a circle1class Circle: # Constructor takes radius as parameter2 def __init__(self, radius): # Store radius with underscore prefix (convention for "private")3 self._radius = radius4 # @property decorator turns this method into a read-only attribute5 @property6 def area(self): # Calculate area: π × r²7 return 3.14 * self._radius ** 28 # Create a circle with radius 59c = Circle(5)# Access area like an attribute (no parentheses needed)10print(c.area)Output
78.5
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this Python code and how often they appear.
_× 6.× 4(× 4)× 4:× 3*× 3=× 2,× 1@× 1