[py-dev] Using funcargs with decorators

Antonio Cuni anto.cuni at gmail.com
Fri Oct 12 11:11:41 CEST 2012


On 10/11/2012 11:53 PM, holger krekel wrote:
> 
> On a sidenote, i am not sure Python's decorator design was such
> a great idea.  Maybe it should have been restricted to setting attributes
> (like C# and also java IIRC) and then a way to get those attributed
> functions on a per-class, per-module or even global basis.

well, there are obviously things which you can't do by just setting
attributes, and moreover nothing would stop people to do the old "fn =
decor(fn)" trick.

I think that in general the *args, **kwargs pattern works well enough. The
only two drawbacks I can see in day-to-day usage are:

- that the name of the decorated function is different than the original one,
although nowadays we have @functools.wraps (but it's still not widely used).

- that decorators are not composable is one wraps and the other sets an
attribute: depending on the order they are applied we might set the attribute
on the wrapper or wrapped object.

For more advanced usages, it would be nice to have an automatic way to "copy"
the signature from the wrapped to the wrapper: this alone would solve both the
pypy and the OP problems.

Additional bonus: a standard way to declare (via a function attribute) that
this function object is a wrapper for this other, to be used e.g. by debuggers
to show the "correct" source code. E.g., I have a "source" command in pdb++
which breaks in case of wrappers:

@mydecor
def foo(...):
    ...

(pdb++) source foo
def mydecor(fn):
    def wrapped(*args, **kwargs):
        return fn(*args, **kwargs)
    return wrapped

ciao,
Anto



More information about the Pytest-dev mailing list