[Python-Dev] Proposed tweaks to functools.wraps (original) (raw)

Steven D'Aprano steve at pearwood.info
Wed Aug 11 15:00:29 CEST 2010


On Wed, 11 Aug 2010 09:26:56 pm Simon Cross wrote:

On Wed, Aug 11, 2010 at 4:39 AM, Benjamin Peterson <benjamin at python.org> wrote: > Namespace conflict with what? I would prefer "wraps" unless it's > standardized as a behavior for all decorators.

Having the original function available as wrapped would be really cool, although I'm not quite sure what the behaviour should be in corner cases like: * The decorator returns the original function (I suppose a reference to itself is okay?)

There's no reason why a function can't have an attribute that refers to the function itself. It works fine:

def f(): ... return f.wrapped ... f.wrapped = f

f() is f True

But in the case of a decorator returning the original function, the point is moot. If the original function is returned, then it hasn't been decorated at all, so there shouldn't be a wrapped attribute added:

def decorator(func): if not debug: # Return undecorated original. return func
@wraps(func) def inner(*args): print args return func(*args) return inner

* The decorator returns the a function that is already decorating something else.

That shouldn't make any difference. Given:

@wraps(f) def func(*args): do_something() return f(*args)

then func.wrapped gives f. If f itself wraps (say) g, and g wraps h, then you have:

func.wrapped => f func.wrapped.wrapped => g func.wrapped.wrapped.wrapped => h

and so on, until you reach a function that doesn't wrap anything and doesn't have a wrapped attribute.

I'm +1 on the proposal.

-- Steven D'Aprano



More information about the Python-Dev mailing list