• Complain

Jacob Zimmerman - Python Descriptors

Here you can read online Jacob Zimmerman - Python Descriptors full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2016, publisher: Apress, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Jacob Zimmerman Python Descriptors
  • Book:
    Python Descriptors
  • Author:
  • Publisher:
    Apress
  • Genre:
  • Year:
    2016
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Python Descriptors: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Python Descriptors" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

This short book on Python descriptors is a collection of knowledge and ideas from many sources on dealing with and creating descriptors. And, after going through the things all descriptors have in common, the author explores ideas that have multiple ways of being implemented as well as completely new ideas never seen elsewhere before.

There arent many good resources out there for writing Python descriptors, and extremely few books. This is a sad state of affairs, as it makes it difficult for Python developers to get a really good understanding of how descriptors work and the techniques to avoid the big gotchas associated with working with them.

This truly is a comprehensive guide to creating Python descriptors. As a BONUS: A pip install-able library, descriptor_tools, was written alongside this book and is an open source library on GitHub.

What youll learn:

What is a descriptor protocol

What is attribute access and how they apply to descriptors

How to make descriptors and why

Which Methods are needed

How to store attributes

How to do Read-Only Descriptors and Writing _delete()

How to explore the descriptor classes

How to apply the other uses of descriptors and more

Who is this book for:

This book is for experienced Python coders, programmers and developers.

Jacob Zimmerman: author's other books


Who wrote Python Descriptors? Find out the surname, the name of the author of the book and a list of all author's works by series.

Python Descriptors — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Python Descriptors" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
Part I
About Descriptors
About Descriptors
This part provides a deep explanation of what descriptors are, how they work, and how theyre used. Enough information will be given to be able to look at any descriptor and understand how it works and why it works that way.
Its not difficult to figure out how to create descriptors from the information given, but little to no guidance is given to help with this. Part 2 will cover that with a bunch of options for creating new descriptors.
Jacob Zimmerman 2016
Jacob Zimmerman Python Descriptors 10.1007/978-1-4842-2505-9_1
1. What Is a Descriptor?
Jacob Zimmerman 1
(1)
Placeholder, New York, USA
Electronic supplementary material
The online version of this chapter (doi: 10.1007/978-1-4842-2505-9_1 ) contains supplementary material, which is available to authorized users.
Put very simply, a descriptor is a class that can be used to call a method with simple dot notation, also referred to as attribute access, but theres obviously more to it than that. Its difficult to really explain beyond that without digging a little into how theyre implemented. So, heres a high-level view of the descriptor protocol.
A descriptor implements at least one of these three methods: __get__() , __set__() , or __delete__() . Each of these methods has a list of parameters that are needed, which will be discussed later in the book, and each is called by a different sort of access of the attribute the descriptor represents. Doing simple a.x access will call the __get__() method of x , setting the attribute using a.x = value will call the __set__() method of x , and using del a.x will call, as expected, the __delete__() method of x .
As stated earlier, only one of the methods needs to be implemented in order to be considered a descriptor, but any number of them can be implemented. And, depending on the descriptor type and which methods are implemented, not implementing certain methods can restrict certain types of attribute access or provide an interesting alternative behavior for them. There are two types of descriptors based on which sets of these methods are implemented: data and nondata.
Data Descriptors vs. Nondata Descriptors
A data descriptor implements at least __set__() or __delete__() , but can include both. They also often include __get__() since its rare to want to set something without also being able to get it too. You can get the value, even if the descriptor doesnt include __get__() , but its either a roundabout process or the descriptor writes it to the instance. That will be discussed more later in the book.
A nondata descriptor only implements __get__() . If it adds __set__() or __delete__() to its method list, it becomes a data descriptor.
Unfortunately, the PyPy interpreter (up to version 2.4.0; the fix is in the next version, which hadnt been released yet at the time of writing) gets this a little bit wrong. It doesnt take __delete__() into consideration until it knows that its a data descriptor, and PyPy doesnt believe something is a data descriptor unless __set__() is implemented. Luckily, since a majority of data descriptors implement __set__() , this rarely becomes a problem.
It may seem like the distinction is pointless, but it is not. It comes into play upon attribute look up. This will be discussed more later in the book, but basically the distinction is based on the types of uses it provides.
The Use of Descriptors by Python
It is worth noting that descriptors are an inherent part of how Python works. Python is known to be a multiparadigm language, and as such it supports paradigms such as functional programming, imperative programming, and object-oriented programming (among others). This book does not attempt to go into depth about the different paradigms, only the object-oriented programming paradigm will be observed. Descriptors are used implicitly in Python for the languages object-oriented mechanisms. As it will be explained in the chapters that follow, methods are implemented using descriptors. As you may guess from reading this, it is because of descriptors that object-oriented programming is possible in Python. Descriptors are very powerful and advanced, and this book aims to teach Python programmers how to use them fully .
Summary
Descriptors occupy a large part of the Python language, as they can replace attribute access with method calls and even restrict which types of attribute access is allowed. Now that you have a broad idea of how descriptors are implemented as well as their use by the language, lets dig a little deeper yet and gain a better understanding of how they work.
Jacob Zimmerman 2016
Jacob Zimmerman Python Descriptors 10.1007/978-1-4842-2505-9_2
2. The Descriptor Protocol
Jacob Zimmerman 1
(1)
Placeholder, New York, USA
In order to get a better idea of what descriptors are good for, it is best to finish showing the full descriptor protocol. Its time to see the full signature of the protocols methods and what the parameters are.
__get__(self, instance, owner)
This method is clearly the method for retrieving whatever data or object the descriptor is meant to maintain. Obviously, self is a parameter, since its a method. Also, it receives instance and/or owner .
Lets start with owner , which is the class that the descriptor is accessed from, or the class of the instance its being accessed from. When you make the call A.x , where x is a descriptor object with __get__() , its called with an owner and the instance is set to None . So the lookup gets effectively transformed into A.__dict__['x'].__get__(None, A) . This lets the descriptor know that __get__() is being called from a class, not an instance. The descriptor owner is also often written to have a default value of None , but thats largely an optimization that only built-in descriptors use.
Now, on to the other parameters. The parameter instance is the instance that the descriptor is being accessed from, if it is being accessed from an instance. As mentioned earlier, if None is passed into instance , the descriptor knows that its being called from the class level. But, if instance is not None , then it tells the descriptor which instance its being called from. So an a.x call will be effectively translated to type(a).__dict__['x'].__get__(a, type(a)) . Notice that it still receives the instances class. Notice also that the call still starts with type(a) , not just a , because descriptors are stored on classes. In order to be able to apply per-instance as well as per-class functionality, descriptors are given instance and owner (the class of the instance). How this translation and application happens will be discussed later in the book.
Remember that this applies to __set__() and __delete__() as well and self is an instance of the descriptor itself. It is not the instance that the descriptor is being called from, but instead, the instance parameter is the instance the descriptor is being called from. This may sound confusing at first, but dont worry if you dont understand for now, everything will be explained further.
The __get__() method is the only one that bothers to get the class separately. Thats because its the only method on nondata descriptors, which are generally made at a class level. The built-in decorator classmethod is implemented using descriptors and the __get__() method. In that case, it will make use of the owner parameter alone.
__set__(self, instance, value)
Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Python Descriptors»

Look at similar books to Python Descriptors. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Python Descriptors»

Discussion, reviews of the book Python Descriptors and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.