Design: Idiom for classes and methods that are customizable by the user?

Michael Selik michael.selik at gmail.com
Tue May 17 14:13:06 EDT 2016


On Tue, May 17, 2016 at 11:18 AM Dirk Bächle <tshortik at gmx.de> wrote:

>
> > It's not so great to require
> > that the user must explicitly ``add`` their derived class after defining
> > it. Perhaps that add function could be a decorator?
>
> Our current API doesn't use decorators at all, since it's also aimed at
> people with no (or only some) knowledge of Python.


I think you'll find that users of decorators do not need any special
knowledge. The Flask framework (http://flask.pocoo.org/) is proof of that.


> decorators...would have looked strange to the average user perhaps?
>

Many beginners are willing to mimic your example code without questioning
what that "@" thingy is doing above the function or class.

Compare that with trying to explain to a C++ user that in Python classes
are actually objects and you can pass them to the ``add`` function to
register them... that's going to take a while.

We're now free to add them into the mix for our new Python 2.7.x/3.y
> codebase, but this would probably require an API discussion
> first.
>

An alternative would be creating a TaskmasterMeta metaclass that registers
the Taskmaster subclass when it's defined. I'm a little reluctant to
recommend that one. I find decorators to be more elegant.



More information about the Python-list mailing list