Using built-in decorators
Python has several built-in decorators that are part of the language. The @property
, @classmethod
, and @staticmethod
decorators are used to annotate methods of a class. The @property
decorator transforms a method function into a descriptor. We use this to give a method function the syntax of a simple attribute. The property decorator, when applied to a method, also creates an additional pair of properties that can be used to create a setter
and deleter
property. We looked at this in Chapter 3, Attribute Access, Properties, and Descriptors.
The @classmethod
and @staticmethod
decorators transform a method function into a class-level function. The decorated method can now be called from a class, not an object. In the case ...
Get Mastering Object-oriented Python now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.