[Python-ideas] Introspecting decorated functions? [was: Tweaking closures and lexical scoping to include the function being defined]

Stephen J. Turnbull stephen at xemacs.org
Sun Oct 2 16:05:44 CEST 2011


Nick Coghlan writes:
 > On Sun, Oct 2, 2011 at 4:16 AM, Stephen J. Turnbull <stephen at xemacs.org> wrote:

 > > Isn't magic needed solely to inject the nonlocal statement(s) into the
 > > definition of FUNC inside <anon1> at compile-time?
 > 
 > Well, having 'FUNC' the same from the compiler's point of view is also
 > necessary to get introspection to work properly (i.e. FUNC.__name__ ==
 > 'FUNC').

Not only isn't that magic, but it doesn't currently work anyway, at
least not for me in Python 3.2 (borrowing Ron's example):

>>> def artdeco(y):
...  def _(func):
...   def wrapper(x):
...    return func(x,y)
...   return wrapper
...  return _
... 
>>> def baz(x, y):
...  return x + y        
... 
>>> baz = artdeco(2)(baz)
>>> baz.__name__
'wrapper'

As expected, but Expedia Per Diem! and

>>> @artdeco(3)
... def quux(x, y):
...  return y - x
... 
>>> quux.__name__
'wrapper'

Woops!  As for the "not magic" claim:

>>> def dc(y):
...  def _(func):
...   def wrapper(x):
...    return func(x,y) 
...   wrapper.__name__ = func.__name__       
...   return wrapper
...  return _
... 
>>> @dc(1)   
... def doom(x, y):
...  return x + y
... 
>>> doom.__name__      
'doom'
>>> 

Should a bug be filed, or is this already part of your "improved
introspection for closures" proposal?




More information about the Python-ideas mailing list