python装饰器
程序员文章站
2022-06-01 20:23:21
...
# coding=utf-8
def print_hi(func):
def inner():
print('hi')
func()
return inner
@print_hi
def print_1():
print(1)
@print_hi
def print_2():
print(2)
print_1()
print_2()
# 运行结果:
# hi
# 1
# hi
# 2
带参数 :
# coding=utf-8
def print_hi(func):
def inner(*args):
print('hi')
func(*args)
return inner
@print_hi
def print_1(ar):
print(1, ar)
@print_hi
def print_2():
print(2)
print_1(55)
print_2()
# 运行结果:
# hi
# 1 55
# hi
# 2
上一篇: 一次误删数据的经历(Mongodb)
下一篇: 一次郁闷的查错经历