Skip to main content
main-content

Über dieses Buch

Create descriptors and see ideas and examples of how to use descriptors effectively. In this short book, you’ll explore descriptors in general, with a deep explanation of what descriptors are, how they work, and how they're used. Once you understand the simplicity of the descriptor protocol, the author delves into using and creating descriptors in practice, with plenty of tips, patterns, and real-world guidance. Because descriptors are inherently flexible, you’ll work with multiple examples illustrating how to best take advantage of them.
This second edition includes additions throughout, including new material covering the set_name_() descriptors, new and improved flowcharts to explain the inner workings of descriptors, and a completely new chapter to address instance-level attributes, the easiest way to create descriptors correctly the first time.
Although brief, Python Descriptors is a comprehensive guide to creating Python descriptors, including a pip install-able library called descriptor_tools, which was written alongside this book and is an open source library on GitHub. After reading this book, you will have a solid understanding of how descriptors work and the techniques to avoid the big gotchas associated with working with them.
What You Will LearnDiscover descriptor protocols
Master attribute access and how it applies to descriptors
Build your own descriptors
Use descriptors to store attributes
Create read-only descriptors
Explore the descriptor classes
Apply the other uses of descriptors

Who This Book Is For
Experienced Python coders, programmers, and developers.

Inhaltsverzeichnis

Frontmatter

About Descriptors

Frontmatter

Chapter 1. What Is a Descriptor?

Put very simply, a descriptor is a class that can be used to call a method with simple attribute access, but there's obviously more to it than that. It's difficult to explain beyond that without digging a little into how descriptors implemented. So, here's a high-level view of the descriptor protocol.
Jacob Zimmerman

Chapter 2. The Descriptor Protocol

In order to get a better idea of what descriptors are good for, let's finish showing the full descriptor protocol. It's time to see the full signatures of the protocol's methods and what the parameters are.
Jacob Zimmerman

Chapter 3. What Are Descriptors Good For?

Nothing is perfect in this world, and Python’s descriptors are no exception. Descriptors allow you to do some pretty cool things, but those cool things come at a cost. Here, we discuss the good and the bad.
Jacob Zimmerman

Chapter 4. Descriptors in the Standard Library

There are three basic, well-known descriptors that come with Python: property, classmethod, and staticmethod. There's also a fourth one that you use all the time, but are less likely to know is a descriptor. In this chapter, we take a good look at all four.
Jacob Zimmerman

Chapter 5. Attribute Access and Descriptors

It was stated earlier that attribute access calls are transformed into descriptor calls, but it was not stated how. The quick answer is that __getattribute__(), __setattr__(), and __delattr__() do it. That probably isn't much of an answer for you, so I'll dig into it more. These three methods exist on all normal objects, inherited via the object class (and classes inherit it from the type metaclass). As you might imagine, these methods are called when an attribute on an object is retrieved, set, or deleted, respectively, and it is these methods that decide whether to use a descriptor, __dict__, or __slots__, and whether to return/set something on the class or on the instance.
Jacob Zimmerman

Making Descriptors

Frontmatter

Chapter 6. Which Methods Are Needed?

When designing a descriptor, it must be decided which methods will be included. It can sometimes help to decide right away if the descriptor should be a data or non-data descriptor, but sometimes it works better to "discover" which kind of descriptor it is.
Jacob Zimmerman

Chapter 7. Storing the Attributes

Now that all the preliminaries are out of the way, it is time to see the part of descriptors that is useful: storing the attributes that the descriptor represents. There are a lot of ways to store attributes with a descriptor, and this chapter will go over every option that I'm aware of, starting with the easiest.
Jacob Zimmerman

Chapter 8. Read-Only Descriptors

There are many good uses for read-only—or immutable—property descriptors. In fact, there is a lot to back up the idea of having everything be effectively immutable. Unfortunately, due to Python's inherent lack of being able to make anything actually immutable, interpreter optimization isn't one of those possible benefits with Python. (PyPy may be able to make JIT optimizations because of it, but don't take my word for it.)
Jacob Zimmerman

Chapter 9. Writing__delete__()

This is going to be a short chapter, since there isn't really all that much to say, but it didn't really fit in any of the other chapters. Also, __get__() and __set__() sort of got their own chapters.
Jacob Zimmerman

Chapter 10. Descriptors Are Classes Too

It's time for some more advanced stuff with descriptors. Actually, it's not really advanced, since it's stuff that pertains to all classes. There won't be a very in-depth look at much in this chapter; it’s just a reminder that features normally available to classes are available to descriptors as well.
Jacob Zimmerman

Chapter 11. Reusing the Wheel

Whenever possible and sensible, one should try to avoid reinventing the wheel. This chapter goes over a set of classes to use as superclasses and strategies to help build new descriptors a little faster. Only barebones code is presented here; the full code examples are in the library.
Jacob Zimmerman

Chapter 12. Instance-Level Descriptors

What's the most confusing part about a property-like data descriptor? Wrapping your head around the fact that it is being used to control instance-distinct attributes from its class.
Jacob Zimmerman

Chapter 13. Other Uses of Descriptors In the World

Much of the usefulness of descriptors covered in this book was just using them as specialized properties. While this is one of the primary purposes of descriptors, it's not all that they can do, though even the more innovative uses still largely serve that purpose.
Jacob Zimmerman

Backmatter

Weitere Informationen

Premium Partner

BranchenIndex Online

Die B2B-Firmensuche für Industrie und Wirtschaft: Kostenfrei in Firmenprofilen nach Lieferanten, Herstellern, Dienstleistern und Händlern recherchieren.

Whitepaper

- ANZEIGE -

Best Practices für die Mitarbeiter-Partizipation in der Produktentwicklung

Unternehmen haben das Innovationspotenzial der eigenen Mitarbeiter auch außerhalb der F&E-Abteilung erkannt. Viele Initiativen zur Partizipation scheitern in der Praxis jedoch häufig. Lesen Sie hier  - basierend auf einer qualitativ-explorativen Expertenstudie - mehr über die wesentlichen Problemfelder der mitarbeiterzentrierten Produktentwicklung und profitieren Sie von konkreten Handlungsempfehlungen aus der Praxis.
Jetzt gratis downloaden!

Bildnachweise