[Python-Dev] PEP 318 - writing to func_name attribute

Guido van Rossum guido at python.org
Fri Aug 6 16:53:51 CEST 2004


> One thing I added awhile back to PEP 318 as an open issue is that for ease
> of writing wrappers the func_name attribute of a function object should be
> writable.  For example, in situations where the decorator returns a new
> function object (often, I would think) it would really be nice if that new
> function object had the same name as the undecorated function.  Consider:
> 
>     def a(func):
>         def _inner(*args, **kwds):
>             return func(*args, **kwds)
>         return _inner
> 
>     @a
>     def func(*args, **kwds):
>         print args
>         print kwds
> 
>     print "func's name:", func.func_name
> 
> I realize you can use new.function() to create a new function object, but
> that seems like a fair amount of extra complexity just to change the
> function's name.  I'd prefer it if the decorator could be written as:
> 
>     def a(func):
>         def _inner(*args, **kwds):
>             return func(*args, **kwds)
>         _inner.func_name = func.func_name
>         return _inner
> 
> That fails because func_name is readonly.  Any chance this restriction can
> be loosened up?

Good idea, independent from decorators; please add it to the PEP!

--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list