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 circle
1class Circle:
# Constructor takes radius as parameter
2 def __init__(self, radius):
# Store radius with underscore prefix (convention for "private")
3 self._radius = radius
4
# @property decorator turns this method into a read-only attribute
5 @property
6 def area(self):
# Calculate area: π × r²
7 return 3.14 * self._radius ** 2
8
# Create a circle with radius 5
9c = Circle(5)
# Access area like an attribute (no parentheses needed)
10print(c.area)

Output

78.5
Practice typing this code

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

More Python examples at level 4

← Python typing practice