Decorators

andrew cooke andrew at acooke.org
Fri Mar 6 09:11:10 EST 2009


the arguments are similar to aspect oriented programming (so you could
google that).

sometimes you want to do a similar thing in various places in your code. 
the classic example is logging when a method is called.  rather than
adding logging statements to every method, you can use a decorator - you
place the logging code in one place (the decorator definition) and then
add the decorator to each method you want to log.


in addition to the aspect oriented approach (or at least extending it from
how it is used in other languages), decorators can take advantage of the
way python is structured to make fundamental changes to how methods work. 
so in python decorators can also do things like define static and class
methods.  so they provide a unified approach to what, in other languages,
tend to be unrelated ideas.

andrew


Johny wrote:
> Hi,
> Can anyone explain to me what are decorators for? What are advantages
> of using them?
> Thanks
> L.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list