[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] @
Okay, think about the function classmethod(). It takes a single
callable as an argument and return a callable. You could write this
def make_from_str(cls, data):
newobj = cls()
newobj.parse(data)
return newobj
make_from_str = classmethod(make_from_str)
inside a class. You could also write this
@classmethod
def make_from_str(cls, data):
newobj = cls()
newobj.parse(data)
return newobj
which does the same thing. Up to you to decide which you think is more
readable. Other uses for decorators include reducing boilerplate for
similar functions, central registration of functions (such as
registering event handlers in a game), and much more.
--Noah
On Dec 31, 2008, at 7:18 PM, Jake b wrote:
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