Jacob Zimmerman
New York, USA
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484237267 . For more detailed information, please visit http://www.apress.com/source-code .
ISBN 978-1-4842-3726-7 e-ISBN 978-1-4842-3727-4
https://doi.org/10.1007/978-1-4842-3727-4
Library of Congress Control Number: 2018960194
Jacob Zimmerman 2018
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
Introduction
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.
Code Samples
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 youre using new style classes.
The Descriptor Tools Library
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. Its an open source project with a public GitHub repository. 1
Note
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.
Conventions in This Book
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.
New in the 2nd Edition
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 , Storing the Attributes.
Another addition is an idea that was inspired by looking into the __set_name__() addition to the protocol, which youll 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.
Acknowledgments
In order to be sure that I got everything rightit would really suck for a comprehensive guide to be missing a big chunk of functionality or to get anything wrongI enlisted the help of some Python experts on the first edition. In return for their help, I let them introduce themselves to you here. Thats not all I did in return, but its all youre 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 whats 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!
https://github.com/Rosuav
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.
Table of Contents
Part I: About Descriptors
Part II: Making Descriptors
About the Author and About the Technical Reviewer
About the Author
Jacob Zimmerman
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.