[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] @



def decorator(f):
   def decorated(*args, **kw):
       print "I have been decorated!", f.__name__
       return f(*args, **kw)

   return decorated

@decorator
def fn(x):
   return x

print fn(5)


I'm too tired to try and explain, hopefully some else will. But I think that is what you were trying to do.

Don't worry decorators are easy as once it clicks.

If it doesn't execute on function call, I don't get what the use is? I
thought if I did this, it would print out "I am decorator"  ( but it
only does it on function declaration )

def dec(f):
    print "I am decorator!", f
    return f

@dec
def fn(x): return x

( And if it doesn't, then the logThisMethodCall() will not be logging
every time it is called, just once on declaration ?: )

On Wed, Dec 31, 2008 at 4:05 PM, Lenard Lindstrom <len-l@xxxxxxxxx> wrote:
An instance of function fn is created, then function decorator is called with that instance as its only argument. > decorator then returns the function instance, which is assigned to identifier fn. So a decorator is called when a > function declaration is executed, not later when the function is called.
--
Jake