• python decorator simple example


    Why we need the decorator in python ?

    Let's see a example:

    #!/usr/bin/python
    
    def fun_1(x):
        return x*2 
    
    def fun_2(x):
        return x*x*2
    
    if __name__ == '__main__':
        print 'call fun_1'
        print fun_1(8)
        print 'call fun_2'
        print fun_2(9)

    We can see more than code heavy:

    so we don't want to write the below code any more ...

    print 'call fun_1'
    print 'call fun_2'

    How can we do it with a simple way !

    Try use decorator:

    #!/usr/bin/python
    
    def fun_decorator(f):
        def fn(x):
            print 'call '+f.__name__
            return f(x)
        return fn
    
    @fun_decorator
    def fun_1(x):
        return x*2 
    @fun_decorator
    def fun_2(x):
        return x*x*2
    
    if __name__ == '__main__':
        print fun_1(8)
        print fun_2(9)

    See the result below:

    But,sometime We are expect to write something before or after tips:

    something like below,we add some at the head ..

    How to make it ?

    a simple and stupid method below:

    #!/usr/bin/python
    
    def fun_decorator(f):
        def fn(x):
            print 'call '+f.__name__
            return f(x)
        return fn
    
    @fun_decorator
    def fun_1(x):
        return x*2 
    @fun_decorator
    def fun_2(x):
        return x*x*2
    
    if __name__ == '__main__':
        print 'INFO',fun_1(8)
        print 'DEBUG',fun_2(9)

    You can see,we are just simply add some string before call function !

    So,How to make it easy ..

    #!/usr/bin/python
    
    def fun_decorator(user_info):
        def wrapper(f):
            def fn(x):
                print '%s call %s' % (user_info,f.__name__)
                return f(x)
            return fn
        return wrapper
    
    @fun_decorator('INFO')
    def fun_1(x):
        return x*2 
    @fun_decorator('DEBUG')
    def fun_2(x):
        return x*x*2
    
    if __name__ == '__main__':
        print fun_1(8)
        print fun_2(9)

    If you function with more than one argument,How can we do ?

    #!/usr/bin/python
    
    def fun_decorator(user_info):
        def wrapper(f):
            def fn(x,y):
                print '%s call %s' % (user_info,f.__name__)
                return f(x,y)
            return fn
        return wrapper
    
    
    @fun_decorator("INFO")    
    def fun_1(x,y):
        return x*y 
    
    @fun_decorator("INFO")
    def fun_2(x,y):
        return x*y*2
    
    if __name__ == '__main__':
    
        print fun_1(2,3)
        print fun_2(2,3)

    Okay,Sometime,we even don't know how many arguments will be input ?

    so,How can we do ?

    >>>we will answer it next time ! Thank you

    Can we drop this masquerade
  • 相关阅读:
    Oracle根据【日期】组,其他条件根据PIVOT行转列。使每个日期条件关联的其他数据只有一行。
    ORACLE数据库,数据量大,转移数据到备份表语句
    C#解析"a=1&b=2&c=3"字符串,微信支付返回字符串,替换<br>为&
    dataTable的数据,调试的时候点放大镜就看到了啊啊啊!
    Debug和Release 老程序啊 调试之前 区分一下啊
    FastReport.NET
    grpc 实现微服务生态笔记
    金木水火土
    shell 指令分析nginx 日志qps
    idea中使用tomcat 方式启动spring boot项目
  • 原文地址:https://www.cnblogs.com/landpack/p/4602019.html
Copyright © 2020-2023  润新知