Python修饰符实践

代码1

 1 def fun_a(ob):
 2     print "hello"
 3     print ob.__doc__
 4     return ob
 5 
 6 @fun_a
 7 def fun_b(a,b):
 8     """this is function b"""
 9     print a+b
10     print "world"
11 
12 fun_b(3,4)

 代码2

 1 def fun_a(ob):
 2     print "hello"
 3     print ob.__doc__
 4     return ob
 5 
 6 def fun_b(a,b):
 7     """this is function b"""
 8     print a+b
 9     print "world"
10 
11 fun_b = fun_a(fun_b)
12 fun_b(3,4)

相同的输出:

1 hello
2 this is function b
3 7
4 world

可以看出,@fun_a与声明fun_b =  fun_a(fun_b)完全等价

原文地址:https://www.cnblogs.com/mess4u/p/2735501.html