普通函数1
import time
def
f1():
print(
'This is a function1')
def
f2():
print(
'This is a fuction2')
def
print_current_time(
fun):
print(time.time())
fun()
print_current_time(f1)
print_current_time(f2)
普通函数2
import time
def
decorator(
fun):
def
wrapper():
print(time.time())
fun()
return wrapper
def
f1():
print(
' This is a function')
f = decorator(f1)
f()
1524504690.4488041
This is a function
函数3 (装饰器)
import time
def
decorator(
fun):
def
wrapper():
print(time.time())
fun()
return wrapper
@decorator
def
f1():
print(
' This is a function')
f(
1)
This is a function
函数3 (装饰器)(一个参数)
import time
def
decorator(
func):
def
wrapper(
func_n):
print(time.time())
func(func_n)
return wrapper
@decorator
def
f1(
func_name):
print(
' This is a function '+func_name)
f1(
'hello')
This is a function hello
函数四(装饰器)(两个参数)
import time
def
decorator(
func):
def
wrapper(*
func_n):
print(time.time())
func(*func_n)
return wrapper
@decorator
def
f1(
func_name):
print(
' This is a function '+func_name)
@decorator
def
f2(
func_name1,
func_name2):
print(
' This is a function2 '+ func_name1)
print(
' This is a function2 '+ func_name2)
f1(
'hello')
f2(
'lalal',
'yyoyo')
This is a function hello
1524505737.8087363
This is a function2 lalal
This is a function2 yyoyo
函数五(装饰器 )(三个函数或以上)
import time
def
decorator(
func):
def
wrapper(*
func_n,**
kw):
print(time.time())
func(*func_n,**kw)
return wrapper
@decorator
def
f1(
func_name): #wrapper(func_n) func(func_n)
print(
' This is a function '+func_name)
@decorator
def
f2(
func_name1,
func_name2): # wrapper(*func_n) func(func_n)
print(
' This is a function2 '+ func_name1)
print(
' This is a function2 '+ func_name2)
@decorator
def
f3
(
func_name1
,
func_name2
,**
kw
):#
wrapper
(*func_n
,**kw
)
func(*func_n,**kw)
print(
' This is a function2 '+ func_name1)
print(
' This is a function2 '+ func_name2)
print(kw)
@decorator
def
f4(
fun1 ,
fun2,
fun3,
fun4): #wrapper(*func_n) func(*func_n)
print(fun1)
print(fun2)
print(fun3)
print(fun4)
f1(
'this is func1')
f2(
'this is func2',
'this is func2222')
f3(
'test func1',
'test func2',
a=
1,
b=
2,
c =
'123')
f4(
'1',
'2',
'3',
'4')
This is a function this is func1
1524506681.5375907
This is a function2 this is func2
This is a function2 this is func2222
1524506681.5375907
This is a function2 test func1
This is a function2 test func2
{'a': 1, 'b': 2, 'c': '123'}
1524506681.5375907
1
2
3
4