Skip to Content
Mastering Object-oriented Python
book

Mastering Object-oriented Python

by Steven F. Lott
April 2014
Beginner to intermediate
634 pages
15h 22m
English
Packt Publishing
Content preview from Mastering Object-oriented Python

Adding method functions to a class

A class decorator creates new method functions using a two-step process: by creating the method function and then inserting it into the class definition. This is often better done via a mixin class than a decorator. The obvious and expected use of a mixin is to insert methods. Inserting methods another way is less obvious and can be astonishing.

In the example of the Total_Ordering decorator, the exact method functions inserted were flexible and depended on what was already provided. This was a kind of special case that was typical but also very clever.

We might want to define a standardized memento() method. We'd like to include this standard method function in a variety of classes. We'll look at the decorator ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Object-Oriented Python - Second Edition

Mastering Object-Oriented Python - Second Edition

Steven F. Lott

Publisher Resources

ISBN: 9781783280971Supplemental Content