Python_装饰器

装饰器:本质是函数,用于装饰其他函数,为其他函数添加附加功能

原则:

1.不能比修改被装饰的函数的代码

2.不能修改被装饰的函数的调用方式

 

import time
def timmer(func):
    def warpper(*args,**kwargs):
        start_time=time.time()
        func()
        stop_time=time.time()
        print(start_time,stop_time)
    return warpper()

@timmer
def test1():
    time.sleep(3)
    print('I am test1 !!!!!')

执行结果:

I am test1 !!!!!
1520652510.288641 1520652513.2888126

实现装饰器知识储备:

装饰器=高阶函数+嵌套函数

1.函数即“变量”

2.高阶函数

  a.把一个函数名当作形参传给另外一个函数(在不修改被装饰函数源代码的情况下,为其添加功能)

  b.返回值中包含函数名(不修改函数的调用方式)

3.嵌套函数

1.函数即"变量"

def foo():
    print('in the foo !!!')
    bar()

def bar():
    print('I am bar !!!')

foo()

执行结果:

in the foo !!!
I am bar !!!
 

def bar():
    print('in the bar !!!')

def test1(func):
    print(func)

test1(bar)

执行结果:

<function bar at 0x01E54228>

 

def bar():
    print('in the bar')

def test1(func):
    print(func)
    func()
test1(bar)

执行结果:

<function bar at 0x02104228>
in the bar

 

import time
def bar():
    print('in the bar !!!')

def test1(func):
    start_time=time.time()
    func()
    stop_time=time.time()
    print('in the fun !!!')
    print(start_time,stop_time)

test1(bar)

执行结果:

in the bar !!!
in the fun !!!
1520656155.743149 1520656155.743149

 

import time

def bar():
    time.sleep(3)

def test2(func):
    print(func)
    return func

print (test2(bar))

执行结果:

<function bar at 0x005C4228>
<function bar at 0x005C4228>

 

import time

def bar():
    time.sleep(3)

def test2(func):
    print(func)
    return func

print (test2(bar))

t=test2(bar)
print(t)

执行结果:

<function bar at 0x01DF4228>
<function bar at 0x01DF4228>
<function bar at 0x01DF4228>
<function bar at 0x01DF4228>

 

import time

def bar():
    time.sleep(3)
    print('in the bar !!!')

def test2(func):
    print(func)
    return func

bar=test2(bar)
bar()

执行结果:

<function bar at 0x00714228>
in the bar !!!

 

def foo():
    print('in the foo')
    def bar():
        print('in the bar')

    bar()

foo()

运行结果:

in the foo
in the bar

 

import time

def deco(func):
    start_time=time.time()
    func()
    stop_time=time.time()

def test1():
    time.sleep(3)
    print('in the test1')

def test2():
    time.sleep(3)
    print('in the test2')


#deco(test1)
test1=deco(test1)
#deco(test2)
test2=deco(test2)

in the test1
in the test2

 

import time

def timer(func):
    def deco():
        start_time=time.time()
        func()
        stop_time=time.time()
        print(start_time,stop_time)
    return deco


def test1():
    time.sleep(3)
    print('in the test1')

def test2():
    time.sleep(3)
    print('in the test2')

print(timer(test1))
test1() #--->执行deco()函数

运行结果:

<function timer.<locals>.deco at 0x005841E0>

 

import time

def timer(func):
    def deco():
        start_time=time.time()
        func()
        stop_time=time.time()
        print(start_time,stop_time)
    return deco

@timer # 等同于 test1=timer(test1)
def test1():
    time.sleep(3)
    print('in the test1')

def test2():
    time.sleep(3)
    print('in the test2')

test1()

运行结果:

in the test1
1520663936.4761815 1520663939.476353

转载于:https://my.oschina.net/u/3125863/blog/1632634

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值