Decorators and how they relate to Python - A little insight please!

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Sat Oct 21 18:08:56 EDT 2006


Jerry a écrit :
> Thanks to everyone that resonded.  I will have to spend some time
> reading the information that you've provided.
> 
> To Fredrik, unfortunately yes.  I saw the examples, but couldn't get my
> head wrapped around their purpose.  Perhaps it's due to the fact that
> my only experience with programming is PHP, Perl and Python and to my
> knowledge only Python supports decorators (though it could be that I
> just didn't encounter them until I came across Python).

Python's "decorators" are just a possible use of "higher order 
functions" (aka HOFs) - functions that takes functions as arguments 
and/or return functions. HOFs are the base of functional programming 
(Haskell, ML, Lisp, etc), and rely on functions being first class 
citizens (which is the case in Python where functions are objects). 
While Python is not truly a functional language, it has a good enough 
support for functional programing, and happily mixes functional 
programming and OO concepts.

If you have no prior exposure to a functional language, it's not 
surprinsing you have some hard time understanding decorators and their 
possible uses. I suggest you read David Mertz's articles on FP in Python:
http://www-128.ibm.com/developerworks/library/l-prog.html

While these articles predate the @decorator syntax (and FWIW makes 
absolutely no mention of decorators), it should help you grasping basic 
Python's functional idioms.

HTH



More information about the Python-list mailing list