r/programming • • Jun 19 '13

Python Descriptors Demystified

http://nbviewer.ipython.org/urls/gist.github.com/ChrisBeaumont/5758381/raw/descriptor_writeup.ipynb
51 Upvotes

6 comments sorted by

View all comments

2

u/dollars2donuts Jun 19 '13

This is the first time I've understood descriptors. The article mentioned that it's hard to find examples in open source projects, are there any at all?

3

u/check3streets Jun 19 '13 edited Jun 19 '13

I believe that Kivy Properties are descriptors (though I've never heard them termed as such). Once you've defined, say:

class MyClass(EventDispatcher):

    a = BoundedNumericProperty(1, min=0, max=100)

A call to " myClassInstance.a = 50 " will be validated. Though validation was not the primary goal; Kivy's properties are bindable and any observers of the Property will now be alerted about the change.

A special DSL called .kv makes those properties reactive, so you can compose expressions that reference them and derive new values. It's pretty cool.