python @

test_\@.py

class myDecorator(object):
def __init__(self, f):
print "inside myDecorator.__init__()"
self.f = f

def __call__(self):
print "inside myDecorator.__call__()"
self.f()

@myDecorator
def aFunction():
print "inside aFunction()"

print "Finished decorating aFunction()"
aFunction()
print '\n'

def entryExit(f):
def new_f():
print "Entering", f.__name__
f()
print "Exited", f.__name__
return new_f

@entryExit
def func1():
print "inside func1()"

func1()

 

执行结果

inside myDecorator.__init__()
Finished decorating aFunction()
inside myDecorator.__call__()
inside aFunction()


Entering func1
inside func1()
Exited func1

posted on 2015-10-14 16:21  DayAfterDay  阅读(142)  评论(0编辑  收藏  举报

导航