[Python-Dev] Proposed tweaks to functools.wraps

Nick Coghlan ncoghlan at gmail.com
Wed Aug 11 23:21:32 CEST 2010


On Thu, Aug 12, 2010 at 12:12 AM, Simon Cross
<hodgestar+pythondev at gmail.com> wrote:
> Yes. But it's more common for the original function to have be
> modified in some way. e.g.:
>
> def autodoc(f):
>    f.__doc__ += document_args(f)
>    return f
>
> @autodoc
> def f(x, y):
>     """Add two numbers"""
>     return x + y
>
> And then f.__wrapped__ is not particularly useful because the original
> function no longer exists and odd things will happen. For example, in
> the code above autodoc(f.__wrapped__).__doc__ will not equal
> f.__doc__.

There's no call to wraps or update_wrapper here, so f.__wrapped__ won't exist.

> I guess my description of the problem wasn't clear. I meant:
>
> def _debug(*args, **kwargs)
>    print args, kwargs
>
> def mock(f):
>    return _debug
>
> @mock
> def plus(a, b):
>   return a + b
>
> @mock
> def prod(a, b):
>    return a * b

Again, without any calls to wraps or update_wrapper, plus.__wrapped__
and prod.__wrapped__ won't exist.

However, as I noted before, these kinds of scenario are the reason we
decided that building this feature directly into the decorator
machinery wasn't a good idea.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list