1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Mon Nov 14 01:01:29 2016 @author: toby """ #知识点:原函数如有形参,装饰器如何应对? #装饰器函数 def outer(fun): #这里的形参fun作用是为接受实参,也就是原函数func1()
def wrapper(strs): #如原函数有形参,那么在装饰器函数里边也许增加一个形参strs(任意命名),用于接收传过来的实参
print '哈哈'
fun(strs) #这里也许增加一个形参
print 'hello world!' #例如再增加一个
return wrapper
@outer def func1(arg): #原函数加个形参arg
print 'this is func1' ,arg
#调用函数时,传入一个字符串作为参数 func1( "my name is tantianran" )
|
本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/1873468