113 . python高级------闭包与装饰器(8)

113 . python高级------闭包与装饰器(8)

python修炼第二十六天

2019年 4月 26日 晴

1.装饰器的运行的顺序(1)

def set_fun1(func1):
	print("set_fun1")

	def call_fun1():
		print("call_fun1")
		func1()

	return call_fun1


# 装饰前的test,这个func
# 装饰后的test,就是调用 call_fun


@set_fun1  # @set_fun1 ===> test = set_fun1(test)
def test():
	print("test")

test()

set_fun1
call_fun1
test

2.装饰器的运行顺序(2)

def set_fun1(func1):
	print("set_fun1")

	def call_fun1():
		print("call_fun1")
		func1()

	return call_fun1


def set_fun2(func2):
	print("set_fun2")

	def call_fun2():
		print("call_fun2")
		func2()

	return call_fun2


# 装饰前的test,这个func
# 装饰后的test,就是调用 call_fun

@set_fun2
@set_fun1  # @set_fun1 ===> test = set_fun1(test)
def test():
	print("test")


test()

set_fun1
set_fun2
call_fun2
call_fun1
test

猜你喜欢

转载自blog.csdn.net/qq_40455733/article/details/89640170
113