Successfully reported this slideshow.
DescriptorsPythons most obscure language feature demystified.In 5 minutes.Chris Beaumont@BeaumontChrisgraduate student, ast...
Learning about descriptorscreates an appreciationfor the elegance of Pythons design.-Raymond Hettinger
Learning about descriptorscreates an appreciationfor the elegance of Pythons design.-Raymond HettingerI DONTGET IT
class Email(object):def __init__(self, sender, subject, message):self.sender_widget = QLineEdit(sender)self.subject_widget...
class Email(object):...def get_subject(self):return self.subject_widget.text()def set_subject(self, subject):self.subject_...
class Email(object):...def get_subject(self):return self.subject_widget.text()def set_subject(self, subject):self.subject_...
PropertiesDisguise methodsas attributeshttp://www.flickr.com/photos/mkapple/741018101/
class Email(object):...@propertydef subject(self):return self.subject_widget.text()@subject.setterdef subject(self, subjec...
e = Email()sub = e.subjecte.subject = hi thereclass Email(object):...@propertydef subject(self):return self.subject_widget...
e = Email()sub = e.subjecte.subject = hi thereclass Email(object):...@propertydef subject(self):return self.subject_widget...
class Email(object):def __init__(self, sender, subject, message):self._sender_widget = QLineEdit(sender)self._subject_widg...
class Email(object):def __init__(self, sender, subject, message):self._sender_widget = QLineEdit(sender)self._subject_widg...
DescriptorsReusable Propertieshttp://www.freeimageslive.co.uk/files/images005/locust_leaf.JPG
class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __get__(self, object):widget =...
class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __get__(self, object):widget =...
e = Email()sender = e.sendere.sender = foo...class TextWrapper(object):def __init__(self, widget_name):self.widget_name = ...
e = Email()sender = e.sendere.sender = fooclass TextWrapper(object):def __init__(self, widget_name):self.widget_name = wid...
e = Email()sender = e.sendere.sender = fooNice.class TextWrapper(object):def __init__(self, widget_name):self.widget_name ...
e = Email()sender = e.sendere.sender = fooNice.Nice.class TextWrapper(object):def __init__(self, widget_name):self.widget_...
e = Email()sender = e.sendere.sender = fooNice.Nice.Meh?class TextWrapper(object):def __init__(self, widget_name):self.wid...
Go forthand refactor
DescriptorRecipeshttp://epicthings.net/wp-content/uploads/2011/09/Cookie-Monster-Cookie.jpg
Instance-specific dataclass Foo(object):x = Descriptor()f1 = Foo()f2 = Foo()f1.x = 5f2.x = 4class Descriptor(object):def __...
Accessing DescriptorMethodsclass Descriptor(object)def __get__(self, instance):if instance == None:return selfdef cool_des...
Python Descriptors Demystified
Upcoming SlideShare
Loading in …5
×

Python Descriptors Demystified

1,265 views

Published on

Published in: Technology
  • Be the first to comment

Python Descriptors Demystified

  1. 1. DescriptorsPythons most obscure language feature demystified.In 5 minutes.Chris Beaumont@BeaumontChrisgraduate student, astrophysicsHarvard University / University of Hawaii
  2. 2. Learning about descriptorscreates an appreciationfor the elegance of Pythons design.-Raymond Hettinger
  3. 3. Learning about descriptorscreates an appreciationfor the elegance of Pythons design.-Raymond HettingerI DONTGET IT
  4. 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 codeinteract withemail data?
  5. 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. 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
  7. 7. PropertiesDisguise methodsas attributeshttp://www.flickr.com/photos/mkapple/741018101/
  8. 8. class Email(object):...@propertydef subject(self):return self.subject_widget.text()@subject.setterdef subject(self, subject):self.subject_widget.setText(subject)...
  9. 9. e = Email()sub = e.subjecte.subject = hi thereclass Email(object):...@propertydef subject(self):return self.subject_widget.text()@subject.setterdef subject(self, subject):self.subject_widget.setText(subject)...
  10. 10. e = Email()sub = e.subjecte.subject = hi thereclass Email(object):...@propertydef subject(self):return self.subject_widget.text()@subject.setterdef subject(self, subject):self.subject_widget.setText(subject)...Nice.
  11. 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)@propertydef sender(self):return self._sender_widget.text()@sender.setterdef sender(self, sender):self._sender_widget.setText(sender)@propertydef subject(self):return self._subject_widget.text()@subject.setterdef subject(self, subject):self._subject_widget.setText(subject)@propertydef message(self):return self._message_widget.text()@message.setterdef message(self, message):self._message_widget.setText(message)
  12. 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)@propertydef sender(self):return self._sender_widget.text()@sender.setterdef sender(self, sender):self._sender_widget.setText(sender)@propertydef subject(self):return self._subject_widget.text()@subject.setterdef subject(self, subject):self._subject_widget.setText(subject)@propertydef message(self):return self._message_widget.text()@message.setterdef message(self, message):self._message_widget.setText(message)Gross
  13. 13. DescriptorsReusable Propertieshttp://www.freeimageslive.co.uk/files/images005/locust_leaf.JPG
  14. 14. class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __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. 15. class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __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.sendere.sender = foo......
  16. 16. e = Email()sender = e.sendere.sender = foo...class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __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. 17. e = Email()sender = e.sendere.sender = fooclass TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __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. 18. e = Email()sender = e.sendere.sender = fooNice.class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __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. 19. e = Email()sender = e.sendere.sender = fooNice.Nice.class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __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. 20. e = Email()sender = e.sendere.sender = fooNice.Nice.Meh?class TextWrapper(object):def __init__(self, widget_name):self.widget_name = widget_namedef __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)
  21. 21. Go forthand refactor
  22. 22. DescriptorRecipeshttp://epicthings.net/wp-content/uploads/2011/09/Cookie-Monster-Cookie.jpg
  23. 23. Instance-specific dataclass Foo(object):x = Descriptor()f1 = Foo()f2 = Foo()f1.x = 5f2.x = 4class Descriptor(object):def __init__(self):self._data = {}def __get__(self, instance):return self._data[instance]
  24. 24. Accessing DescriptorMethodsclass Descriptor(object)def __get__(self, instance):if instance == None:return selfdef cool_descriptor_method(self):passdesc = Foo.x # instance = Nonedesc.cool_descriptor_method()

×