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.

2

u/hugosenari Jun 20 '13

Python dbus does When you connect to some dbus service and get a object, this object (a dbus proxy object) don't know object attrs or methods then this dbusObject.foo use descriptors to find and call attr foo of dbus service

My descritive dbus lib does https://github.com/hugosenari/pydbusdecorator I'm use this in similar way as author use NonNegative class but in my specific case (create descritive interfaces) I use decorators

Django Mapping mapping atttr probabily does instance of some data object don't to get all attrs values of object, but need to known how to get coll X from table Y that what can they do only when use 'foo.bar'