美文网首页
python decorator snappet 1

python decorator snappet 1

作者: capcat | 来源:发表于2018-03-29 00:52 被阅读0次

    1.什么是decorator?

    装饰器就是,装饰你目标的东西。其实wapper来理解更好~就好比钢铁侠的盔甲!

    def decorator(fun):
        def warrper(name):
            start = time.time()
            fun(name)
            runtime = time.time() - start
    
            print runtime
        return warrper
    
    @decorator
    def do_something(name):
        for i in range(10000000):
            pass
        print 'name:',name
    
    if __name__ == '__main__':
        do_something('Li yun long')
    
    name: Li yun long
    0.27599978447
    

    让参数不固定:

    def decorator(fun):
        def wapper(*args,**kwargs):
            start = time.time()
            fun(*args,**kwargs)
            runtime = time.time() - start
            print runtime
        return wapper
    
    @decorator
    def do_something(user,name):
        for i in range(10000000):
            pass
        print "user:",user
        print "name:",name
    
    if __name__ == '__main__':
        do_something('Li yun long','yun long')
    
    user: Li yun long
    name: yun long
    0.30999994278
    

    改装一下,让它带参数,
    ···
    def decorator(num):
    def _decorator(fun):
    def wapper(args,kwargs):
    start = time.time()
    for i in xrange(num):
    fun(
    args,**kwargs)
    runtime = time.time() - start
    print runtime
    return wapper
    return _decorator

    @decorator(2)
    def do_something(user,name):
    for i in range(10000000):
    pass
    print "user:",user
    print "name:",name

    if name == 'main':
    do_something('Li yun long','yun long')
    ···

    user: Li yun long
    name: yun long
    user: Li yun long
    name: yun long
    0.5
    
    尬图1.png

    不要试图在里面些两个,第二个不会运行的.


    尬图2.png

    那么,调用顺序呢?

    def decorator(fun):
        print "decorator"
        def warrper():
            print "wapper"
            start = time.time()
            fun()
            runtime = time.time() - start
            print runtime
        return warrper
    
    @decorator
    def do_something():
        for i in range(10000000):
            pass
        print 'do_something'
    

    它的结果是:


    尬图3.png

    被装饰器装饰过的函数名:

    def decorator(fun):
       #print "decorator"
       def warrper(name):
           #print "wapper"
           start = time.time()
           fun(name)
           runtime = time.time() - start
           print runtime
       return warrper
    
    
    @decorator
    def do_something(name):
       for i in range(10000000):
           pass
       print 'do_something',name
    
    if __name__ == '__main__':
       print do_something.__name__
    
    尬图4.png

    用functools:

    import functools
    def decorator(fun):
       @functools.wraps(fun)
       def warrper(name):
           start = time.time()
           fun(name)
           runtime = time.time() - start
           print runtime
       return warrper
    
    @decorator
    def do_something(name):
       for i in range(10000000):
           pass
       print 'do_something',name
    
    if __name__ == '__main__':
       print do_something.__name__
    
    尬图5.png

    相关文章

      网友评论

          本文标题:python decorator snappet 1

          本文链接:https://www.haomeiwen.com/subject/bfmdcftx.html