码迷,mamicode.com
首页 > 编程语言 > 详细

python :practice integrated decorator three categories

时间:2020-03-15 22:19:20      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:pytho   str   tar   world   n+1   RoCE   turn   factor   cti   

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.

           

 

python :practice integrated decorator three categories

标签:pytho   str   tar   world   n+1   RoCE   turn   factor   cti   

原文地址:https://www.cnblogs.com/alansuny/p/12500215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!