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

Data descriptors

Now, let's look at the difference of using a data descriptor. For this, we are going to create another simple descriptor that implements the __set__ method:

class DataDescriptor:    def __get__(self, instance, owner):        if instance is None:            return self        return 42    def __set__(self, instance, value):        logger.debug("setting %s.descriptor to %s", instance, value)        instance.__dict__["descriptor"] = valueclass ClientClass:    descriptor = DataDescriptor()

Let's see what the value of the descriptor returns:

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

Now, let's try to change this value to something else, and see what it returns instead:

>>> client.descriptor = 99>>> client.descriptor42

The value returned by the descriptor didn't ...

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