Docorator Disected
M.E.Farmer
mefjr75 at hotmail.com
Sat Apr 2 17:53:22 EST 2005
Hello Ron ,
You have many good explanations already, but I thought that this
__might__ help others.
Like you I was confused by the decorator syntax. till I realized it was
shorthand for ...
def identity(f):
return f
def foo():
pass
# this is the 'old way'
foo = identity(foo)
It just rebinds foo to the return value of the decorator function.
With the new syntax it becomes.
def identity(f):
return f
@identity
def foo(self):
pass
This is the same as above but now the function is
passed and rebound behind the scenes.
Also note that decorators don't have to be a nested function, it really
depends on what you are trying to achieve.
hth,
M.E.Farmer
More information about the Python-list
mailing list