Python is a remarkable language with many surprisingly powerful features baked into it. Generators, metaclasses, and decorators are some of those, but this book is all about descriptors.
All code samples are written in Python 3, since that is the most recent version, but all the ideas and principles taught in this book apply to Python 2 as well, as long as you’re using new style classes.
Written alongside this book was a library, called descriptor-tools, which can be installed with pip. It contains the fruition of a lot of the ideas and helpers to make it easier to implement them all. It’s an open source project with a public GitHub repository. 1
Superscript letters like the one at the end of the previous line are in reference to the bibliography at the back of the book, which includes URLs to the referenced site.
When the text mentions “class” and “instance” in a general sense, they refer to a class that has a descriptor attribute and to instances of such classes, respectively. All other classes and instances will be referred to more specifically.
The 2 nd edition is an update including new features of Python as well as new ideas to learn. One of the new things is incredibly important if this book wants to maintain the status of “comprehensive” guide that it strives for. This important addition is about the addition of __set_name__() to the descriptor protocol in Python 3.6. You can read about this in Chapter 7 , “Storing the Attributes”.
Another addition is an idea that was inspired by looking into the __set_name__() addition to the protocol, which you’ll see just after the section on that addition. Also, I added a chapter on creating instance-level descriptors, which were added to descriptor-tools well before this edition really got started.
The next thing is actually a change, not an addition. Since writing the first book, I found out about the built-in function vars() . Calling vars(obj) is equivalent to obj.__dict__ , but is more Pythonic. Kind of like calling len(obj) instead of obj.__len__() . So the code examples have been updated to use vars() . Any remaining references to __dict__ are purposeful.
Pretty much everything else new in this edition is just cleaning up the language to be more legible.
In order to be sure that I got everything right—it would really suck for a “comprehensive guide” to be missing a big chunk of functionality or to get anything wrong—I enlisted the help of some Python experts on the first edition. In return for their help, I let them introduce themselves to you here. That’s not all I did in return, but it’s all you’re going to see:)
Emanuel Barry is a self-taught Python programmer who loves pushing the language to its limits as well as exploring its darkest corners. He has to do a lot of proofreading and editing for a local non-for-profit organization, and decided to combine his love of Python and knowledge sharing with his background in proofreading to help make this book even better. He can often be found in the shadows of the mailing lists or the issue tracker, as well as the Python IRC channel, as Vgr.
Chris Angelico has played around with Python since the late 90s, getting more serious with the language in the mid 2000s. As a PEP Editor and active participant in the various mailing lists, he keeps well up to date with what’s new and upcoming in the language and also shares that knowledge with fledgling students in the Thinkful tutoring/mentoring program. When not coding in Python, he is often found wordsmithing for a Dungeons & Dragons campaign, or exploring the linguistic delights of Alice in Wonderland and similar works. If you find a subtle Alice reference in this text, blame him!
Kevin Mackay is a software engineer who has been programming in Python since 2010 and is currently working at BBC, improving the Taster platform. He is enthusiastic about open source software and occasionally contributes to the 3D graphics application, Blender. He can be found on the Python IRC channel as yakca or hiking on a mountain somewhere in Scotland.
is a blogger, gamer (tabletop more so than video games), and programmer who was born and raised in Wisconsin. He has a twin brother who could also be considered to have all those traits.
Jacob has his own programming blog that focuses on Java, Kotlin, and Python programming, called “Programming Ideas with Jake”. He also writes for a gaming blog with his brother-in-law called the “Ramblings of Jacob and Delos”.
His brother writes a JavaScript blog called JoeZimJS and works with his best friend on a gaming YouTube channel called “Bork & Zim Gaming,” which Jacob helps out with on occasion.
Programming Ideas with Jake
http://programmingideaswithjake.wordpress.com/
Ramblings of Jacob and Delos
http://www.ramblingsofjacobanddelos.com/
JoeZimJS
has worked in software development for more than 20 years as an individual contributor, team lead, program manager, and vice president of engineering. Michael has more than 10 years of experience working with mobile devices. His current focus is in the medical sector, using mobile devices to accelerate information transfer between patients and health care providers.