[Tutor] real world decorators
Eike Welk
eike.welk at gmx.net
Mon Sep 21 00:43:32 CEST 2009
On Sunday 20 September 2009, John wrote:
> Hi,
>
> I think I understand what decorators are and how they work. Maybe
> it's just me but I don't know where I'd use them in my real world
> programming. I see how they work with profile or coverage but does
> anyone have real world uses.
Frequently used are @classmethod and @staticmethod:
http://docs.python.org/library/functions.html#classmethod
An other interesting usage of decorators is Phillip J.
Eby's 'simplegeneric' library, where decorated functions replace big
if... elif... constructions:
http://cheeseshop.python.org/pypi/simplegeneric/0.6
But really decorators are just syntactical sugar. They are an elegant
way to write a special case of a function call.
Kind regards,
Eike.
More information about the Tutor
mailing list