S
Steven D'Aprano
Suppose I have a function f() which I know has been decorated, but I don't
have access to the original undecorated function any longer:
def reverse(func):
def f(*args):
args = list(args)
args.reverse()
return func(*args)
return f
def say(*args):
print args
rsay = reverse(say)
del say
Is there any way to peek inside the decorated function rsay() to get access
to the undecorated function say()?
If I look at the code object I can see a reference to the original:
('list', 'args', 'reverse', 'func')
and if I disassemble the code object I can see it being dereferenced:
[snip for brevity]
5 22 LOAD_DEREF 0 (func)
25 LOAD_FAST 0 (args)
28 CALL_FUNCTION_VAR 0
31 RETURN_VALUE
but if I look at the closure object, nothing seems useful:
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__']
and I can't find any other attributes which refers back to the undecorated
original function.
have access to the original undecorated function any longer:
def reverse(func):
def f(*args):
args = list(args)
args.reverse()
return func(*args)
return f
def say(*args):
print args
rsay = reverse(say)
del say
Is there any way to peek inside the decorated function rsay() to get access
to the undecorated function say()?
If I look at the code object I can see a reference to the original:
('list', 'args', 'reverse', 'func')
and if I disassemble the code object I can see it being dereferenced:
[snip for brevity]
5 22 LOAD_DEREF 0 (func)
25 LOAD_FAST 0 (args)
28 CALL_FUNCTION_VAR 0
31 RETURN_VALUE
but if I look at the closure object, nothing seems useful:
['__class__', '__cmp__', '__delattr__', '__doc__',dir(rsay.func_closure[0])
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__']
and I can't find any other attributes which refers back to the undecorated
original function.