Decorating methods - where do my arguments go?
mikael at isy.liu.se
Tue May 12 12:04:39 CEST 2009
Michele Simionato wrote:
> Still it turns something which is a function into an object and
> you lose the docstring and the signature. pydoc will not be too
> happy with this approach.
Duncan Booth wrote:
> I don't know why Mikael wants to use a class rather than a function
> but if he wants to save state between calls this isn't going to help.
I've never used decorators before, and that's the main reason for my
confusion. I have found myself in a situation where I think that
decorators is the way to go. So I tried to find information about how to
write them the usual way: Googling and browsing documentation. I found
examples using both functions and classes, but not much explanation of
what's going on, especially not in the class case. I didn't find any
arguments for favouring any of those alternatives. Actually, most
examples I found did not even include the definition of the actual
decorator or any information about where they might come from, only the
Based on the above observations, I assumed that both alternatives were
equally possible. The first answers I got here also led me to believe
that that was the case. My understanding is that in most cases, you can
use a callable object instead of a function with little or no practical
difference, except the syntax of its definition. In my eyes, a class
definition looks nicer and is more readable than a nested function.
Therefore, I tend to define a class with a __call__ method and helper
methods instead of nested functions, if I find the need.
Practicality beats purity, and it seem like using a class for decoration
is a lot more hassle than using a function. I fold.
More information about the Python-list