While we're on this kind of topic what is the best approach in python
to creating code macros? That is ability to write a higher level
language (or script) which translates down into python? Decorators
look like they would be part of that story. Is that the best way or
is their a more general way?
Thanks,
Lin
p.s. I used to write code macros in LISP... context I am coming from.
----- Original Message ----- From: "Michael George"
<mdgeorge@xxxxxxxxxxxxxx
>
To: <pygame-users@xxxxxxxx>
Sent: Wednesday, December 31, 2008 9:56 AM
Subject: Re: [pygame] @
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.