python二重装饰器初步理解

def first(f):
	print f.__name__,'call first()'
	def fn_1():
		print f.__name__,'call first()_ fn_1()'
		return f() 
	return fn_1
def second(f):
	print f.__name__,'call second()'
	def fn_2():
		print f.__name__,'call second()_fn_2()'
		return f()
	return fn_2
def test():
	print 'test'
test = first(second(test))
test()

  

def first(f):
	print f.__name__,'call first()'
	def fn_1():
		print f.__name__,'call first()_ fn_1()'
		return f() 
	return fn_1
def second(f):
	print f.__name__,'call second()'
	def fn_2():
		print f.__name__,'call second()_fn_2()'
		return f()
	return fn_2
@first
@second
def test():
	print 'test'
test()

  第一种是对第二种的解释

运行结果

为什么 fn_2 call first_fn_1()与 test call second_fn_2()都会运行呢?

此时的fn_2 与test分别是传入first和second的变量f的名字

 

posted @ 2016-07-20 20:23  冷漠脸  阅读(267)  评论(0编辑  收藏  举报