Skip to main content

2018 | Buch

Python Descriptors

Understanding and Using the Descriptor Protocol

insite
SUCHEN

Ü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?
Abstract
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
Abstract
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?
Abstract
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
Abstract
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
Abstract
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?
Abstract
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
Abstract
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
Abstract
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__()
Abstract
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
Abstract
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
Abstract
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
Abstract
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
Abstract
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
Metadaten
Titel
Python Descriptors
verfasst von
Jacob Zimmerman
Copyright-Jahr
2018
Verlag
Apress
Electronic ISBN
978-1-4842-3727-4
Print ISBN
978-1-4842-3726-7
DOI
https://doi.org/10.1007/978-1-4842-3727-4