Python Descriptors by Jacob Zimmerman

By Jacob Zimmerman

This brief ebook on Python descriptors is a set of data and concepts from many assets on facing and developing descriptors. And, after facing the issues all descriptors have in universal, the writer explores rules that experience a number of methods of being carried out in addition to thoroughly new principles by no means visible somewhere else before.

This actually is a finished consultant to making Python descriptors. As an advantage: A pip install-able library, descriptor_tools, was once written along this e-book and is an open resource library on GitHub.

There are not many strong assets available in the market for writing Python descriptors, and intensely few books. this can be a unhappy situation, because it makes it tricky for Python builders to get a very sturdy knowing of the way descriptors paintings and the suggestions to prevent the massive gotchas linked to operating with them.
What you are going to Learn

  • Discover descriptor protocols
  • Master characteristic entry and the way it applies to descriptors
  • Make descriptors and become aware of why you should
  • Store attributes
  • Create read-only descriptors and _delete()
  • Explore the descriptor classes
  • Apply the opposite makes use of of descriptors and more

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

Show description

Read or Download Python Descriptors PDF

Best python books

Fundamentals of Python: From First Programs through Data Structures

In basics OF PYTHON: FROM FIRST courses via information buildings, Washington and Lee college professor Kenneth A. Lambert offers all the vital issues in CS1 and CS2 in a single quantity. This low-cost structure presents teachers with a constant method of instructing introductory programming and knowledge constructions over a regular two-term direction series.

Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython

Python for information research is anxious with the nuts and bolts of manipulating, processing, cleansing, and crunching facts in Python. it's also a pragmatic, sleek advent to medical computing in Python, adapted for data-intensive functions. it is a e-book in regards to the components of the Python language and libraries you'll have to successfully clear up a large set of information research difficulties.

Python and AWS

When you intend to exploit Amazon internet prone (AWS) for distant computing and garage, Python is a perfect programming language for constructing functions and controlling your cloud-based infrastructure. This cookbook will get you all started with greater than dozen recipes for utilizing Python with AWS, in response to the author’s boto library.

Artificial Intelligence with Python

Construct real-world man made Intelligence functions with Python to intelligently engage with the realm round you approximately This e-book Step into the fantastic global of clever apps utilizing this accomplished advisor input the realm of synthetic Intelligence, discover it, and create your individual functions paintings via easy but insightful examples that may get you up and operating with synthetic Intelligence very quickly Who This booklet Is For This e-book is for Python builders who are looking to construct real-world synthetic Intelligence purposes.

Extra resources for Python Descriptors

Sample text

No, to truly keep users from assigning new values, __set__() is required, but it obviously can't do its normal job. So, what can it do? It can raise an exception. AttributeError is probably the best option of the built-in exceptions, but the functionality is almost unique enough to make a custom exception. It's up to you, but the examples use AttributeError. Now that the attribute can't be changed, how does one supply it with its original value? Trying to send it in through the descriptor's constructor would simply end up with the same value for every instance.

Often, this case pops up because the descriptor is purposely storing the attribute under its own name, which is almost guaranteed to prevent name conflicts. But it's still possible that an outside data descriptor has the same name as where the main descriptor is trying to store its data. In order to avoid this, it is preferable to always directly reference the instance's __dict__. Another good reason is that it makes it more explicit and obvious where the data is being stored. The next thing to be figured out is how the descriptor knows where to store the data.

Set-it-and-forget-it Descriptors Now set-it-and-forget-it descriptors can finally be explained. Of the three methods in the descriptor protocol, these descriptors generally only implement __set__(), as seen in the example. That's not always the case, though. For example the following lazy initialization descriptor only uses __get__(). __name__] = value return value This lazy descriptor is also a decorator over a function, which it replaces and uses to do the lazy initialization. In this case, and in the case of other set-it-and-forget-it descriptors, the descriptor sets the value directly onto the instance, using the same name the descriptor is referenced by.

Download PDF sample

Rated 4.31 of 5 – based on 41 votes