Property decorators are a useful feature in Python that allow you to define methods as “properties” of a class. A property is a method that is accessed like an attribute, but is actually a function that is called under the hood.
Using Property Decorators
To define a property in a class, you can use the built-in property
function as a decorator. For example:
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@property
def age(self):
return self._age
# create a Person object
p = Person('John', 30)
# access the name and age properties
print(p.name) # Output: 'John'
print(p.age) # Output: 30
In this example, the name
and age
methods are defined as properties using the @property
decorator. These properties can be accessed like attributes, but are actually called under the hood when accessed.
You can also define “setter” and “deleter” methods for properties, using the @<property>.setter
and @<property>.deleter
decorators, respectively. For example:
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@property
def age(self):
return self._age
@age.setter
def age(self, value):
self._age = value
# create a Person object
p = Person('John', 30)
# set the name and age properties
p.name = 'Jane'
p.age = 40
# access the name and age properties
print(p.name) # Output: 'Jane'
print(p.age) # Output: 40
In this example, the name
and age
properties have setter methods that are defined using the @<property>.setter
decorator. These setter methods allow you to set the value of the properties.
You can also define deleter methods for properties using the @<property>.deleter
decorator. For example:
class Person:
def __init__(self, name, age):
self._name = name
self._age = age
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@name.deleter
def name(self):
del self._name
@property
def age(self):
return self._age
@age.setter
def age(self, value):
self._age = value
@age.deleter
def age(self):
del self._age
# create a Person object
p = Person('John', 30)
# delete the name and age properties
del p.name
del p.age
# try to access the name and age properties
print(p.name) # Output: AttributeError: 'Person' object has no attribute '_name'
print(p.age) # Output: AttributeError: 'Person' object has no attribute '_age'
In this example, the name
and age
properties have deleter methods that are defined using the @<property>.deleter
decorator. These deleter methods allow you to delete the properties using the del
statement.
Conclusion
Property decorators are a useful feature in Python that allow you to define methods as “properties” of a class, and to define setter and deleter methods for those properties. They can be used to create more intuitive and expressive code, and are a powerful tool for object-oriented programming in Python.
Exercises
To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.