Property Decorator
Part of the Object Oriented Programming section of Coddy's Python journey — lesson 10 of 64.
The @property decorator allows you to access methods like attributes, providing a clean way to get and set values.
Here is an example of a class using @property:
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@property
def area(self):
return 3.14159 * self._radius ** 2Create a circle and access properties:
my_circle = Circle(5)Access properties like regular attributes:
print(my_circle.radius)
print(my_circle.area)Output:
5
78.53975Notice you don't use parentheses () when accessing properties - they look like attributes but run method code.
Add a setter to allow changing values:
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@radius.setter
def radius(self, value):
if value > 0:
self._radius = value
else:
print("Radius must be positive!")Now you can set the radius like an attribute:
my_circle = Circle(5)
my_circle.radius = 10 # Uses the setter
print(my_circle.radius) # Uses the getterOutput:
10Key Point: @property makes methods look like attributes, while @property_name.setter allows you to control how values are assigned.
Cheat sheet
The @property decorator allows you to access methods like attributes, providing a clean way to get and set values.
Basic property usage:
class Circle:
def __init__(self, radius):
self._radius = radius
@property
def radius(self):
return self._radius
@property
def area(self):
return 3.14159 * self._radius ** 2Access properties without parentheses:
my_circle = Circle(5)
print(my_circle.radius) # 5
print(my_circle.area) # 78.53975Add a setter to control value assignment:
@radius.setter
def radius(self, value):
if value > 0:
self._radius = value
else:
print("Radius must be positive!")Set values like attributes:
my_circle.radius = 10 # Uses the setter
print(my_circle.radius) # Uses the getterTry it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of OOP
External FilesIntroduction to OOPClasses vs ObjectsThe self ParameterMethodsAttributesConstructor Method (__init__)Recap - Simple Calculator4Inheritance
Basic InheritanceThe super() FunctionMethod OverridingMultiple InheritanceMethod Resolution OrderRecap - Employee Hierarchy7Special Methods
Magic Methods IntroductionOperator OverloadingContainer Magic MethodsRecap - Custom List10Design Patterns Part 1
Intro to design patternSingleton PatternFactory PatternObserver PatternStrategy Pattern2Decorators
Introduction to DecoratorsProperty DecoratorStatic Method DecoratorClass Method Decorator5Polymorphism
Method Overriding RevisitedDuck TypingAbstract Classes and MethodsInterface DesignRecap - Shape Calculator8Advanced OOP Concepts
Composition vs InheritanceMixinsStatic and Class MethodsClass DecoratorsContext Managers3Class Properties
Instance vs Class VariablesProperty DecoratorsPrivate AttributesRecap - Bank Account Manager6Encapsulation
Public, Protected, Private MemAccess ModifiersInformation HidingProperty Decorators AdvancedRecap - Student Records System12Project: Library Management
Project OverviewBook and User Classes