SlideShare a Scribd company logo
Descriptors
Python's most obscure language feature demystified.
In 5 minutes.
Chris Beaumont
@BeaumontChris
graduate student, astrophysics
Harvard University / University of Hawaii
Learning about descriptors
creates an appreciation
for the elegance of Python's design.
-Raymond Hettinger
Learning about descriptors
creates an appreciation
for the elegance of Python's design.
-Raymond Hettinger
I DON'T
GET IT
class Email(object):
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
How does other code
interact with
email data?
class Email(object):
...
def get_subject(self):
return self.subject_widget.text()
def set_subject(self, subject):
self.subject_widget.setText(subject)
...
e = Email()
e.set_subject('Hi there')
sub = e.get_subject()
class Email(object):
...
def get_subject(self):
return self.subject_widget.text()
def set_subject(self, subject):
self.subject_widget.setText(subject)
...
e = Email()
e.set_subject('Hi there')
sub = e.get_subject()
Gross
Properties
Disguise methods
as attributes
http://www.flickr.com/photos/mkapple/741018101/
class Email(object):
...
@property
def subject(self):
return self.subject_widget.text()
@subject.setter
def subject(self, subject):
self.subject_widget.setText(subject)
...
e = Email()
sub = e.subject
e.subject = 'hi there'
class Email(object):
...
@property
def subject(self):
return self.subject_widget.text()
@subject.setter
def subject(self, subject):
self.subject_widget.setText(subject)
...
e = Email()
sub = e.subject
e.subject = 'hi there'
class Email(object):
...
@property
def subject(self):
return self.subject_widget.text()
@subject.setter
def subject(self, subject):
self.subject_widget.setText(subject)
...
Nice.
class Email(object):
def __init__(self, sender, subject, message):
self._sender_widget = QLineEdit(sender)
self._subject_widget = QLineEdit(subject)
self._message_widget = QLineEdit(message)
@property
def sender(self):
return self._sender_widget.text()
@sender.setter
def sender(self, sender):
self._sender_widget.setText(sender)
@property
def subject(self):
return self._subject_widget.text()
@subject.setter
def subject(self, subject):
self._subject_widget.setText(subject)
@property
def message(self):
return self._message_widget.text()
@message.setter
def message(self, message):
self._message_widget.setText(message)
class Email(object):
def __init__(self, sender, subject, message):
self._sender_widget = QLineEdit(sender)
self._subject_widget = QLineEdit(subject)
self._message_widget = QLineEdit(message)
@property
def sender(self):
return self._sender_widget.text()
@sender.setter
def sender(self, sender):
self._sender_widget.setText(sender)
@property
def subject(self):
return self._subject_widget.text()
@subject.setter
def subject(self, subject):
self._subject_widget.setText(subject)
@property
def message(self):
return self._message_widget.text()
@message.setter
def message(self, message):
self._message_widget.setText(message)
Gross
Descriptors
Reusable Properties
http://www.freeimageslive.co.uk/files/images005/locust_leaf.JPG
class TextWrapper(object):
def __init__(self, widget_name):
self.widget_name = widget_name
def __get__(self, object):
widget = getattr(object, self.widget_name)
return widget.text()
def __set__(self, object, value):
widget = getattr(object, self.widget_name)
widget.setText(value)
class Email(object):
sender = TextWrapper('sender_widget')
subject = TextWrapper('subject_widget')
message = TextWrapper('message_widget')
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
...
...
class TextWrapper(object):
def __init__(self, widget_name):
self.widget_name = widget_name
def __get__(self, object):
widget = getattr(object, self.widget_name)
return widget.text()
def __set__(self, object, value):
widget = getattr(object, self.widget_name)
widget.setText(value)
class Email(object):
sender = TextWrapper('sender_widget')
subject = TextWrapper('subject_widget')
message = TextWrapper('message_widget')
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
e = Email()
sender = e.sender
e.sender = 'foo'
...
...
e = Email()
sender = e.sender
e.sender = 'foo'
...
class TextWrapper(object):
def __init__(self, widget_name):
self.widget_name = widget_name
def __get__(self, object):
widget = getattr(object, self.widget_name)
return widget.text()
def __set__(self, object, value):
widget = getattr(object, self.widget_name)
widget.setText(value)
class Email(object):
sender = TextWrapper('sender_widget')
subject = TextWrapper('subject_widget')
message = TextWrapper('message_widget')
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
e = Email()
sender = e.sender
e.sender = 'foo'
class TextWrapper(object):
def __init__(self, widget_name):
self.widget_name = widget_name
def __get__(self, object):
widget = getattr(object, self.widget_name)
return widget.text()
def __set__(self, object, value):
widget = getattr(object, self.widget_name)
widget.setText(value)
class Email(object):
sender = TextWrapper('sender_widget')
subject = TextWrapper('subject_widget')
message = TextWrapper('message_widget')
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
e = Email()
sender = e.sender
e.sender = 'foo'
Nice.
class TextWrapper(object):
def __init__(self, widget_name):
self.widget_name = widget_name
def __get__(self, object):
widget = getattr(object, self.widget_name)
return widget.text()
def __set__(self, object, value):
widget = getattr(object, self.widget_name)
widget.setText(value)
class Email(object):
sender = TextWrapper('sender_widget')
subject = TextWrapper('subject_widget')
message = TextWrapper('message_widget')
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
e = Email()
sender = e.sender
e.sender = 'foo'
Nice.
Nice.
class TextWrapper(object):
def __init__(self, widget_name):
self.widget_name = widget_name
def __get__(self, object):
widget = getattr(object, self.widget_name)
return widget.text()
def __set__(self, object, value):
widget = getattr(object, self.widget_name)
widget.setText(value)
class Email(object):
sender = TextWrapper('sender_widget')
subject = TextWrapper('subject_widget')
message = TextWrapper('message_widget')
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
e = Email()
sender = e.sender
e.sender = 'foo'
Nice.
Nice.
Meh?class TextWrapper(object):
def __init__(self, widget_name):
self.widget_name = widget_name
def __get__(self, object):
widget = getattr(object, self.widget_name)
return widget.text()
def __set__(self, object, value):
widget = getattr(object, self.widget_name)
widget.setText(value)
class Email(object):
sender = TextWrapper('sender_widget')
subject = TextWrapper('subject_widget')
message = TextWrapper('message_widget')
def __init__(self, sender, subject, message):
self.sender_widget = QLineEdit(sender)
self.subject_widget = QLineEdit(subject)
self.message_widget = QLineEdit(message)
Go forth
and refactor
Descriptor
Recipes
http://epicthings.net/wp-content/uploads/2011/09/Cookie-Monster-Cookie.jpg
Instance-specific data
class Foo(object):
x = Descriptor()
f1 = Foo()
f2 = Foo()
f1.x = 5
f2.x = 4
class Descriptor(object):
def __init__(self):
self._data = {}
def __get__(self, instance):
return self._data[instance]
Accessing Descriptor
Methods
class Descriptor(object)
def __get__(self, instance):
if instance == None:
return self
def cool_descriptor_method(self):
pass
desc = Foo.x # instance = None
desc.cool_descriptor_method()

More Related Content

What's hot

Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
Sujith Kumar
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
Aleksander Fabijan
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Building a Pluggable Plugin
Building a Pluggable PluginBuilding a Pluggable Plugin
Building a Pluggable PluginBrandon Dove
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
jQuery+Drupal Optimizations
jQuery+Drupal OptimizationsjQuery+Drupal Optimizations
jQuery+Drupal Optimizations
Helior Colorado
 
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
Lucas Arruda
 
Doctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 ParisDoctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 Parispablodip
 
Doctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San FranciscoDoctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San Franciscopablodip
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
Lalitkumar_98
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
Raghunath A
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
Sujith Kumar
 
0php 5-online-cheat-sheet-v1-3
0php 5-online-cheat-sheet-v1-30php 5-online-cheat-sheet-v1-3
0php 5-online-cheat-sheet-v1-3Fafah Ranaivo
 
아이폰강의(3)
아이폰강의(3)아이폰강의(3)
아이폰강의(3)sunwooindia
 
Delegate
DelegateDelegate
Delegate
rsvermacdac
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
Sasidhar Kothuru
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
Marco Vito Moscaritolo
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
Ramasubbu .P
 

What's hot (20)

Advance OOP concepts in Python
Advance OOP concepts in PythonAdvance OOP concepts in Python
Advance OOP concepts in Python
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
Building a Pluggable Plugin
Building a Pluggable PluginBuilding a Pluggable Plugin
Building a Pluggable Plugin
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
jQuery+Drupal Optimizations
jQuery+Drupal OptimizationsjQuery+Drupal Optimizations
jQuery+Drupal Optimizations
 
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics1st CI&T Lightning Talks: Writing better code with Object Calisthenics
1st CI&T Lightning Talks: Writing better code with Object Calisthenics
 
Doctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 ParisDoctrator Symfony Live 2011 Paris
Doctrator Symfony Live 2011 Paris
 
Doctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San FranciscoDoctrator Symfony Live 2011 San Francisco
Doctrator Symfony Live 2011 San Francisco
 
CLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHONCLASS OBJECT AND INHERITANCE IN PYTHON
CLASS OBJECT AND INHERITANCE IN PYTHON
 
Python – Object Oriented Programming
Python – Object Oriented Programming Python – Object Oriented Programming
Python – Object Oriented Programming
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
0php 5-online-cheat-sheet-v1-3
0php 5-online-cheat-sheet-v1-30php 5-online-cheat-sheet-v1-3
0php 5-online-cheat-sheet-v1-3
 
아이폰강의(3)
아이폰강의(3)아이폰강의(3)
아이폰강의(3)
 
Delegate
DelegateDelegate
Delegate
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 

Similar to Python Descriptors Demystified

Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
Koteswari Kasireddy
 
Empower your App by Inheriting from Odoo Mixins
Empower your App by Inheriting from Odoo MixinsEmpower your App by Inheriting from Odoo Mixins
Empower your App by Inheriting from Odoo Mixins
Odoo
 
Pyimproved again
Pyimproved againPyimproved again
Pyimproved again
rik0
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
Joshua Forman
 
DOM and Events
DOM and EventsDOM and Events
DOM and Events
Julie Iskander
 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
Koteswari Kasireddy
 
Python-oop
Python-oopPython-oop
Python-oop
RTS Tech
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2Graham Dumpleton
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
tcsonline1222
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)MongoSF
 
Improving Correctness with Types Kats Conf
Improving Correctness with Types Kats ConfImproving Correctness with Types Kats Conf
Improving Correctness with Types Kats Conf
Iain Hull
 
Powerful Generic Patterns With Django
Powerful Generic Patterns With DjangoPowerful Generic Patterns With Django
Powerful Generic Patterns With Django
Eric Satterwhite
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
tuan vo
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Edureka!
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocolrocketcircus
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocolrocketcircus
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
prasnt1
 

Similar to Python Descriptors Demystified (20)

Object_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdfObject_Oriented_Programming_Unit3.pdf
Object_Oriented_Programming_Unit3.pdf
 
Empower your App by Inheriting from Odoo Mixins
Empower your App by Inheriting from Odoo MixinsEmpower your App by Inheriting from Odoo Mixins
Empower your App by Inheriting from Odoo Mixins
 
Pyimproved again
Pyimproved againPyimproved again
Pyimproved again
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in Python
 
DOM and Events
DOM and EventsDOM and Events
DOM and Events
 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptx
 
Python-oop
Python-oopPython-oop
Python-oop
 
PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2PyCon US 2012 - State of WSGI 2
PyCon US 2012 - State of WSGI 2
 
cbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptxcbse class 12 Python Functions2 for class 12 .pptx
cbse class 12 Python Functions2 for class 12 .pptx
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
 
Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)Ruby Development and MongoMapper (John Nunemaker)
Ruby Development and MongoMapper (John Nunemaker)
 
Improving Correctness with Types Kats Conf
Improving Correctness with Types Kats ConfImproving Correctness with Types Kats Conf
Improving Correctness with Types Kats Conf
 
Powerful Generic Patterns With Django
Powerful Generic Patterns With DjangoPowerful Generic Patterns With Django
Powerful Generic Patterns With Django
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in python
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocol
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocol
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdf
 
JavaScript
JavaScriptJavaScript
JavaScript
 

Recently uploaded

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 

Python Descriptors Demystified

  • 1. Descriptors Python's most obscure language feature demystified. In 5 minutes. Chris Beaumont @BeaumontChris graduate student, astrophysics Harvard University / University of Hawaii
  • 2. Learning about descriptors creates an appreciation for the elegance of Python's design. -Raymond Hettinger
  • 3. Learning about descriptors creates an appreciation for the elegance of Python's design. -Raymond Hettinger I DON'T GET IT
  • 4. class Email(object): def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message) How does other code interact with email data?
  • 5. class Email(object): ... def get_subject(self): return self.subject_widget.text() def set_subject(self, subject): self.subject_widget.setText(subject) ... e = Email() e.set_subject('Hi there') sub = e.get_subject()
  • 6. class Email(object): ... def get_subject(self): return self.subject_widget.text() def set_subject(self, subject): self.subject_widget.setText(subject) ... e = Email() e.set_subject('Hi there') sub = e.get_subject() Gross
  • 8. class Email(object): ... @property def subject(self): return self.subject_widget.text() @subject.setter def subject(self, subject): self.subject_widget.setText(subject) ...
  • 9. e = Email() sub = e.subject e.subject = 'hi there' class Email(object): ... @property def subject(self): return self.subject_widget.text() @subject.setter def subject(self, subject): self.subject_widget.setText(subject) ...
  • 10. e = Email() sub = e.subject e.subject = 'hi there' class Email(object): ... @property def subject(self): return self.subject_widget.text() @subject.setter def subject(self, subject): self.subject_widget.setText(subject) ... Nice.
  • 11. class Email(object): def __init__(self, sender, subject, message): self._sender_widget = QLineEdit(sender) self._subject_widget = QLineEdit(subject) self._message_widget = QLineEdit(message) @property def sender(self): return self._sender_widget.text() @sender.setter def sender(self, sender): self._sender_widget.setText(sender) @property def subject(self): return self._subject_widget.text() @subject.setter def subject(self, subject): self._subject_widget.setText(subject) @property def message(self): return self._message_widget.text() @message.setter def message(self, message): self._message_widget.setText(message)
  • 12. class Email(object): def __init__(self, sender, subject, message): self._sender_widget = QLineEdit(sender) self._subject_widget = QLineEdit(subject) self._message_widget = QLineEdit(message) @property def sender(self): return self._sender_widget.text() @sender.setter def sender(self, sender): self._sender_widget.setText(sender) @property def subject(self): return self._subject_widget.text() @subject.setter def subject(self, subject): self._subject_widget.setText(subject) @property def message(self): return self._message_widget.text() @message.setter def message(self, message): self._message_widget.setText(message) Gross
  • 14. class TextWrapper(object): def __init__(self, widget_name): self.widget_name = widget_name def __get__(self, object): widget = getattr(object, self.widget_name) return widget.text() def __set__(self, object, value): widget = getattr(object, self.widget_name) widget.setText(value) class Email(object): sender = TextWrapper('sender_widget') subject = TextWrapper('subject_widget') message = TextWrapper('message_widget') def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message) ... ...
  • 15. class TextWrapper(object): def __init__(self, widget_name): self.widget_name = widget_name def __get__(self, object): widget = getattr(object, self.widget_name) return widget.text() def __set__(self, object, value): widget = getattr(object, self.widget_name) widget.setText(value) class Email(object): sender = TextWrapper('sender_widget') subject = TextWrapper('subject_widget') message = TextWrapper('message_widget') def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message) e = Email() sender = e.sender e.sender = 'foo' ... ...
  • 16. e = Email() sender = e.sender e.sender = 'foo' ... class TextWrapper(object): def __init__(self, widget_name): self.widget_name = widget_name def __get__(self, object): widget = getattr(object, self.widget_name) return widget.text() def __set__(self, object, value): widget = getattr(object, self.widget_name) widget.setText(value) class Email(object): sender = TextWrapper('sender_widget') subject = TextWrapper('subject_widget') message = TextWrapper('message_widget') def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message)
  • 17. e = Email() sender = e.sender e.sender = 'foo' class TextWrapper(object): def __init__(self, widget_name): self.widget_name = widget_name def __get__(self, object): widget = getattr(object, self.widget_name) return widget.text() def __set__(self, object, value): widget = getattr(object, self.widget_name) widget.setText(value) class Email(object): sender = TextWrapper('sender_widget') subject = TextWrapper('subject_widget') message = TextWrapper('message_widget') def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message)
  • 18. e = Email() sender = e.sender e.sender = 'foo' Nice. class TextWrapper(object): def __init__(self, widget_name): self.widget_name = widget_name def __get__(self, object): widget = getattr(object, self.widget_name) return widget.text() def __set__(self, object, value): widget = getattr(object, self.widget_name) widget.setText(value) class Email(object): sender = TextWrapper('sender_widget') subject = TextWrapper('subject_widget') message = TextWrapper('message_widget') def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message)
  • 19. e = Email() sender = e.sender e.sender = 'foo' Nice. Nice. class TextWrapper(object): def __init__(self, widget_name): self.widget_name = widget_name def __get__(self, object): widget = getattr(object, self.widget_name) return widget.text() def __set__(self, object, value): widget = getattr(object, self.widget_name) widget.setText(value) class Email(object): sender = TextWrapper('sender_widget') subject = TextWrapper('subject_widget') message = TextWrapper('message_widget') def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message)
  • 20. e = Email() sender = e.sender e.sender = 'foo' Nice. Nice. Meh?class TextWrapper(object): def __init__(self, widget_name): self.widget_name = widget_name def __get__(self, object): widget = getattr(object, self.widget_name) return widget.text() def __set__(self, object, value): widget = getattr(object, self.widget_name) widget.setText(value) class Email(object): sender = TextWrapper('sender_widget') subject = TextWrapper('subject_widget') message = TextWrapper('message_widget') def __init__(self, sender, subject, message): self.sender_widget = QLineEdit(sender) self.subject_widget = QLineEdit(subject) self.message_widget = QLineEdit(message)
  • 22.
  • 24. Instance-specific data class Foo(object): x = Descriptor() f1 = Foo() f2 = Foo() f1.x = 5 f2.x = 4 class Descriptor(object): def __init__(self): self._data = {} def __get__(self, instance): return self._data[instance]
  • 25. Accessing Descriptor Methods class Descriptor(object) def __get__(self, instance): if instance == None: return self def cool_descriptor_method(self): pass desc = Foo.x # instance = None desc.cool_descriptor_method()