Property Decorator in Python
Level 4162 keystrokes
This uses the @property decorator to create a computed attribute that acts like a regular attribute.
Python code
# Define a Circle class1class Circle: # The constructor receives the radius2 def __init__(self, radius): # Store it in _radius (underscore: internal by convention)3 self._radius = radius4 # @property lets area be read like an attribute5 @property6 def area(self): # Compute πr² with π approximated as 3.147 return 3.14 * self._radius ** 28 # Create a circle with radius 59c = Circle(5)# c.area has no parentheses: 3.14 * 25 = 78.510print(f"Area: {c.area}")Output
Area: 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)× 4*× 3"× 2=× 2,× 1{× 1