Skip to Content
Clean Code in Python
book

Clean Code in Python

by Mariano Anaya
August 2018
Intermediate to advanced
332 pages
9h 12m
English
Packt Publishing
Content preview from Clean Code in Python

Non-data descriptors

We will start with a descriptor that only implements the __get__ method, and see how it is used:

class NonDataDescriptor:    def __get__(self, instance, owner):        if instance is None:            return self        return 42class ClientClass:    descriptor = NonDataDescriptor()

As usual, if we ask for the descriptor, we get the result of its __get__ method:

>>> client = ClientClass()>>> client.descriptor42

But if we change the descriptor attribute to something else, we lose access to this value, and get what was assigned to it instead:

>>> client.descriptor = 43>>> client.descriptor43

Now, if we delete the descriptor, and ask for it again, let's see what we get:

>>> del client.descriptor>>> client.descriptor42

Let's rewind what just happened. When ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Clean Code in Python - Second Edition

Clean Code in Python - Second Edition

Mariano Anaya
Python for Programmers

Python for Programmers

Paul Deitel, Harvey Deitel

Publisher Resources

ISBN: 9781788835831Supplemental Content