码迷,mamicode.com
首页 > 编程语言 > 详细

python二重装饰器初步理解

时间:2016-07-20 21:30:15      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

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的名字

 

python二重装饰器初步理解

标签:

原文地址:http://www.cnblogs.com/ylw666/p/5689606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!