In “What’s the difference between classmethod and staticmethod in Python?”
we looked at static and class methods.
The third big decorator is the @property decorator
import pytest
class Person:
def __init__(self, name):
self._name = name
@property
def name(self):
print('Getting name')
return self._name
@name.setter
def name(self, value):
print(f'Setting name to {value}')
self._name = value
@name.deleter
def name(self):
print('Deleting name')
del self._name
@pytest.mark.order(1)
def test_getter():
my_person = Person("Joern")
assert my_person.name == "Joern"
@pytest.mark.order(2)
def test_setter():
my_person = Person("Joern")
my_person.name = "Bjoern"
assert my_person.name == "Bjoern"
@pytest.mark.order(3)
def test_deleter():
my_person = Person("Joern")
with pytest.raises(AttributeError):
del my_person.name
assert my_person.name