Встроенные декораторы
1. @staticmethod — статический метод
@staticmethod — статический методclass MathUtils:
@staticmethod
def add(a, b):
return a + b
print(MathUtils.add(2, 3)) # 52. @classmethod — метод класса
@classmethod — метод классаclass User:
def __init__(self, name):
self.name = name
@classmethod
def from_fullname(cls, fullname):
first, last = fullname.split()
return cls(first)
user = User.from_fullname("John Smith")
print(user.name) # John3. @property — превращает метод в атрибут
@property — превращает метод в атрибут4. @<property>.setter и @<property>.deleter
@<property>.setter и @<property>.deleter5. @functools.lru_cache (технически из functools, но часто относят к "встроенным")
@functools.lru_cache (технически из functools, но часто относят к "встроенным")6. @dataclass (из dataclasses)
@dataclass (из dataclasses)7. @abstractmethod (из abc)
@abstractmethod (из abc)Last updated