SlideShare a Scribd company logo
1 of 25
Download to read offline
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 PythonSujith Kumar
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی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 PythonSujith Kumar
 
jQuery+Drupal Optimizations
jQuery+Drupal OptimizationsjQuery+Drupal Optimizations
jQuery+Drupal OptimizationsHelior 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 CalisthenicsLucas 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 PYTHONLalitkumar_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 PythonSujith 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
 
Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .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.pdfKoteswari 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 MixinsOdoo
 
Pyimproved again
Pyimproved againPyimproved again
Pyimproved againrik0
 
Declarative Data Modeling in Python
Declarative Data Modeling in PythonDeclarative Data Modeling in Python
Declarative Data Modeling in PythonJoshua Forman
 
Python_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxPython_Object_Oriented_Programming.pptx
Python_Object_Oriented_Programming.pptxKoteswari Kasireddy
 
Python-oop
Python-oopPython-oop
Python-oopRTS 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
 
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.pptxsantoshkumar811204
 
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 ConfIain Hull
 
Powerful Generic Patterns With Django
Powerful Generic Patterns With DjangoPowerful Generic Patterns With Django
Powerful Generic Patterns With DjangoEric Satterwhite
 
Introduce oop in python
Introduce oop in pythonIntroduce oop in python
Introduce oop in pythontuan 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 Threeamiable_indian
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocolrocketcircus
 
Descriptor Protocol
Descriptor ProtocolDescriptor Protocol
Descriptor Protocolrocketcircus
 
Functions2.pdf
Functions2.pdfFunctions2.pdf
Functions2.pdfprasnt1
 
Implementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord modelsImplementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord modelsKostyantyn Stepanyuk
 

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
 
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
 
Implementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord modelsImplementation of EAV pattern for ActiveRecord models
Implementation of EAV pattern for ActiveRecord models
 

Recently uploaded

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Recently uploaded (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

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()