[Python-ideas] Default decorator?

Arnaud Delobelle arno at marooned.org.uk
Thu Jan 17 10:46:16 CET 2008

python-ideas at python.org
On 17 Jan 2008, at 01:05, Christian Heimes wrote:

> Guido van Rossum wrote:
>> Peter Norvig suggested an idea to me over lunch: a "default
>> decorator". This would be something you could set once per module and
>> it would be invoked for each function definition in the same way as a
>> decorator is invoked, before any explicit decorators. His use case  
>> was
>> something that wraps every function that uses argument annotations
>> with something that interprets those annotations in a certain way and
>> enforces that interpretation. It would save having to explicitly
>> annotate every function or method that way.
>> Thoughts? I suggested that a metaclass could do this for methods, but
>> that of course leaves plain functions in the lurch.
> Is it really required? As it is trivial to write a meta class to
> decorator all members of a class, it is also trivial to write a  
> function
> which accepts a module name and decorate all functions in the module.
> def decorate_functions(decorator, modname):
>   function = type(decorate_functions)
>   namespace = sys.modules[modname].__dict__
>   for name, obj in namespace.items():
>       if isinstance(obj, function):
>           namespace[name] = decorator(obj)
> decorate_functions(somefunc, __name__)

This doesn't work as decorators don't usually commute.  If the module  
contains a function:

def bar(...)

decorate_functions will change it to:

def bar(...)

Whereas the desired result is:

def bar(...)


More information about the Python-ideas mailing list