[Python-Dev] @decoration of classes (original) (raw)
Michael Chermside mcherm at mcherm.com
Mon Mar 28 19:25:18 CEST 2005
- Previous message: [Python-Dev] Re: [Python-checkins] python/dist/src/Python thread_pthread.h, 2.53, 2.53.4.1
- Next message: [Python-Dev] @decoration of classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Josiah Carlson writes:
[... stuff about reST and TeX ...]
While I have not used it often, I have done the equivalent of decorating classes; it is as natural (though perhaps not quite as useful initially) as decorating functions, [... stuff about ice cream and sprinkles ...]
Hmm... the only bit that I found particularly interesting there was the bit where you mention that you've used class decorators (without the syntax) before.
What did you use them for? After all, the current state of things is "don't bother implementing class decorators because there is no compelling use case". If you've got some sample use cases, what were they?
For my own part, I observe the following. Because a function decorator acts after the function object is created, there are limits to what it can do to the function. It can add some markup (eg: set properties or doc strings). It can "hook" either before or after the function is called. Or it can "veto" the function call and do something else instead. In the case of function calls, these are pretty much the interesting things you would want to do.
Similarly, because a class decorator acts after the class is created there are limits on what it can do. It can modify the class object (replacing methods and such). It can add markup. It can replace the class with another (perhaps a proxy or some such). But most of these are things which are more easily done by a metaclass... and all of them can be done by metaclasses. The only noticable advantage that I see to class decorators over metaclasses is that there's a more straightforward way to combine them. And I'm not sure that combining metaclasses (or class decorators) is something I want to encourage.
So I'm inclined to use different tools for modifying functions and modifying classes because the ways you want to modify them are different, and decorators are "tuned" to what people normally want to do with functions (like simple wrapping) while metaclasses are "tuned" to what people normally want to do with classes (like support for inheritance.
But a few good use cases would change my mind.
-- Michael Chermside
- Previous message: [Python-Dev] Re: [Python-checkins] python/dist/src/Python thread_pthread.h, 2.53, 2.53.4.1
- Next message: [Python-Dev] @decoration of classes
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]