python 修饰符(转载)

首先一个修饰符的实例:

 #!/usr/bin/env python  

 def run(fn):
def do_hello():
print "begin..."
fn()
print "end."
return do_hello @run
def hello():
print "Hello World!" if __name__ == '__main__':
hello()

修饰符@也可也可以用多個,先加 @run_b 再加@run

@run

@run_b

def hello():

print "hello world!"

上面的例子是在function中使用修饰符,也可以在class中使用修饰符。

 class entryExit(object):  

     def __init__(self, f):
self.f = f def __call__(self):
print "Entering", self.f.__name__
self.f()
print "Exited", self.f.__name__ @entryExit
def func1():
print "inside func1()" @entryExit
def func2():
print "inside func2()"
上一篇:idempotence


下一篇:【转载】 python修饰符@