[Tutor] Decorators

Kent Johnson kent37 at tds.net
Tue Apr 11 12:15:54 CEST 2006


Alan Gauld wrote:
>> My problem, and this is after reading PEP 318 and other items found when I
>> "Googled" for decorators, is that I can't figure out the practical use for
> 
> There is no practical use for decorators IMHO
> They are syntactic sugar added to the language to make some things
> that were already possible a little tidier looking (things like class/static
>  methods etc)

It's true that decorators are syntactic sugar and don't add any new 
functional capabilities to the language.

@foo
def bar():
   pass

is equivalent to

def bar():
   pass
bar = foo(bar)

However this doesn't mean that there is no practical use for decorators. 
After all, list comprehension is mostly syntactic sugar too! Decorators 
add expressiveness and clarity to Python rather than functionality. If 
you read the PEP you see this is the primary motivation.

Decorators are used widely so apparently many people agree that they add 
to the expressiveness of the language.

Here is another example - decorators are used in the implementation of a 
proposal for dynamic function overloading:
http://www.artima.com/weblogs/viewpost.jsp?thread=155514

Kent



More information about the Tutor mailing list