Book Image

Mastering Object-oriented Python

By : Steven F. Lott, Steven F. Lott
Book Image

Mastering Object-oriented Python

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (26 chapters)
Mastering Object-oriented Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Some Preliminaries
Index

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 of a static method, there's no explicit reference to the class. With a class method, on the other hand, the class is the first argument of the method function. The following is an example of a class that includes @staticmethod and some @property definitions...