Online Book Reader

Home Category

Learning Python - Mark Lutz [526]

By Root 1697 0
provides access interception for only one specific attribute—we can’t catch every attribute fetch with a single property or descriptor. On the other hand, properties and descriptors handle both attribute fetch and assignment by design: __getattr__ and __getattribute__ handle fetches only; to intercept assignments as well, __setattr__ must also be coded. The implementation is also different: __getattr__ and __getattribute__ are operator overloading methods, whereas properties and descriptors are objects manually assigned to class attributes.

No it isn’t. To quote from Python namesake Monty Python’s Flying Circus:An argument is a connected series of statements intended to establish a

proposition.

No it isn't.

Yes it is! It's not just contradiction.

Look, if I argue with you, I must take up a contrary position.

Yes, but that's not just saying "No it isn't."

Yes it is!

No it isn't!

Yes it is!

No it isn't. Argument is an intellectual process. Contradiction is just

the automatic gainsaying of any statement the other person makes.

(short pause)

No it isn't.

It is.

Not at all.

Now look...

Chapter 38. Decorators

In the advanced class topics chapter of this book (Chapter 31), we met static and class methods and took a quick look at the @ decorator syntax Python offers for declaring them. We also met function decorators briefly in the prior chapter (Chapter 37), while exploring the property built-in’s ability to serve as a decorator, and in Chapter 28 while studying the notion of abstract superclasses.

This chapter picks up where the previous decorator coverage left off. Here, we’ll dig deeper into the inner workings of decorators and study more advanced ways to code new decorators ourselves. As we’ll see, many of the concepts we studied in earlier chapters, such as state retention, show up regularly in decorators.

This is a somewhat advanced topic, and decorator construction tends to be of more interest to tool builders than to application programmers. Still, given that decorators are becoming increasingly common in popular Python frameworks, a basic understanding can help demystify their role, even if you’re just a decorator user.

Besides covering decorator construction details, this chapter serves as a more realistic case study of Python in action. Because its examples are somewhat larger than most of the others we’ve seen in this book, they better illustrate how code comes together into more complete systems and tools. As an extra perk, much of the code we’ll write here may be used as general-purpose tools in your day-to-day programs.

What’s a Decorator?

Decoration is a way to specify management code for functions and classes. Decorators themselves take the form of callable objects (e.g., functions) that process other callable objects. As we saw earlier in this book, Python decorators come in two related flavors:

Function decorators do name rebinding at function definition time, providing a layer of logic that can manage functions and methods, or later calls to them.

Class decorators do name rebinding at class definition time, providing a layer of logic that can manage classes, or the instances created by calling them later.

In short, decorators provide a way to insert automatically run code at the end of function and class definition statements—at the end of a def for function decorators, and at the end of a class for class decorators. Such code can play a variety of roles, as described in the following sections.

Managing Calls and Instances

For example, in typical use, this automatically run code may be used to augment calls to functions and classes. It arranges this by installing wrapper objects to be invoked later:

Function decorators install wrapper objects to intercept later function calls and process them as needed.

Class decorators install wrapper objects to intercept later instance creation calls and process them as required.

Decorators achieve these effects by automatically rebinding function and class names to other callables, at the end of def and class

Return Main Page Previous Page Next Page

®Online Book Reader