Michael Phipps wrote:
Yanom -
A decorator is a method that takes another method as a parameter so that
it can do something. It is usually used for aspect oriented programming.
For example:
def logThisMethodCall(methodCall)
# Do some logging here
@logThisMethodCall
def myMethod(a,b,c)
# do Somthing in here
Now, whenever you call "myMethod", logThisMethodCall gets called first,
with the invocation of myMethod passed into it. You can use it for
logging, security (i.e. does this person have permission to be calling
this), etc.
Michael
This isn't quite right. logThisMethodCall doesn't get called when you
call myMethod, but when you _define_ myMethod. So to fix this example:
def logThisMethodCall(methodCall):
def result(a,b,c):
# do some logging here
methodCall(a,b,c)
return result
@logThisMethodCall
def myMethod(a,b,c):
# do something here
When you define myMethod, logThisMethodCall is called to create a
replacement function for myMethod. it returns a new function "result"
which gets called instead of the original myMethod whenever someone calls
myMethod. result does some logging and then calls the orginal myMethod.
--Mike