On 10/11/2013 4:17 AM, Terry Reedy wrote:
On 10/10/2013 11:13 PM, Cameron Simpson wrote:
On 11Oct2013 02:55, Steven D'Aprano
<steve+comp.lang.pyt...@pearwood.info> wrote:

def undecorate(f):
     """Return the undecorated inner function from function f."""
     return f.func_closure[0].cell_contents

Whereas this feels like black magic. Is this portable to any decorated
function? If so, I'd have hoped it was in the stdlib. If not: black
magic.

And in use:

py> f(100)
201
py> undecorate(f)(100)
200

All lovely, provided you can convince me that undecorate() is robust.
(And if you can, I'll certainly be filing it away in my funcutils
module for later use.)

It only works if the decorator returns a closure with the original
function as the first member (of func_closure). Often true, but not at
all a requirement.

Another standard decorator method is to write a class with a .__call__ method and attach the original function to instances as an attribute. (Indeed, decorators were borrowed from class-happy Java ;-). But there is no standard as to what the function attribute of instances is called. The OP's request for accessing the function without modifying the tested code cannot be met in general. One must have access to the tested code.


--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to