zoukankan      html  css  js  c++  java
  • python :practice integrated decorator three categories

    python decorator is a crucial(vital,signifiant) process.

    decorator mail two category

    1:principal function no arguments(parameters):

     for example:

     principal function:

    decorator:

    import time

    def logger(flag):

         def decorator():

             def  inner():

              stat_time=time.time()

                func()

               end_time=time_time()

               print(end_time-start_time)

               if flag='true':

                  print('date is ')

             return inner

       return decorator

    @logger('true')

    def test():

          print('hello world')

    2:principal fuction have arguments:

    def logger(flag):

         def decorator():

             def  inner(n):

              stat_time=time.time()

                func(n)

               end_time=time_time()

               print(end_time-start_time)

               if flag='true':

                  print('date is ')

             return inner

       return decorator

    from functools import reduce

    @logger('true')

    def factorial_array(n):

        number=reduce(lambda x,y:x*y,range(1,n+1))

        print(number)

    factorial_array(100)

    3:principal fuction have incert arguments:

    def logger(flag):

         def decorator():

             def  inner( *args,**kwargs):

              stat_time=time.time()

                func(*args,**kwargs)

               end_time=time_time()

               print(end_time-start_time)

               if flag='true':

                  print('date is ')

             return inner

       return decorator

    def add(*args):

        number=0

        for i in args:

           number+=i

         print(number)

    add(1,2,3,4)

    decorator  integrated ,function domain,higher-order function ,closure operation.

               

  • 相关阅读:
    提纲挈领webrtc之vad检测
    提纲挈领webrtc音频处理算法之写在前面的话
    搭建git远程服务器三步骤
    详解m4文件
    chrome浏览器被reimage pair 劫持怎么处理
    linux查看系统32位还是64位
    git gc和fsck的用法
    ubuntu 16.04 的64位 安装arm-none-linux-gnueabi-gcc的步骤和问题解决
    利用终端命令实现进入ntfs分区有两种方法。
    ubuntu-14.10下,安装gcc交叉编译器遇到问题及解决方法
  • 原文地址:https://www.cnblogs.com/alansuny/p/12500215.html
Copyright © 2011-2022 走看看