博客
关于我
九.Python_装饰器
阅读量:321 次
发布时间:2019-03-04

本文共 1240 字,大约阅读时间需要 4 分钟。

Python_装饰器

import time #导入时间模块#18.装饰器  函数#作用:不修改目标函数,给目标函数附加不具有的功能    #原则    #不改变目标函数源代码    #不改变目标函数的调用方式#装饰器=高阶函数+函数嵌套+闭包    #获取函数运行时间的装饰器函数        # def timer(func):#获取函数        #     def wrapper(*args,**kwargs):  #获取不定向方法参数        #         start_time=time.time()#获取开始时间        #         res=func(*args,**kwargs)    #执行函数        #         stop_time=time.time()#获取结束时间        #         print("函数运行时间为:%s"%(stop_time-start_time))#输出花耗时间        #         return res#输出返回值        #     return wrapper#将装饰器函数作为返回值返回给目标函数    #循环函数并且在函数前后打印的装饰器        # def sn(max):#接收@sn的参数        #     def sn(fun):#接收函数        #         def wraper(*args,**kwargs):#接收函数不定向参数        #             res=0        #             for i in range(max):#循环函数        #                 print("First.....")        #                 res+=fun(*args,**kwargs)        #                 print("End.......")        #             return res  #返回res接收的函数        #         return wraper  #返回wraper函数        #     return sn #返回_sn函数    #测试函数        # @sn(2)#循环两次cale函数        # @timer#调用装饰器函数        # def cale(l):        #     res=0        #     for i in l:        #         time.sleep(0.1)        #         res+=i        #     return res        #        # print(cale(range(20)))

 

转载地址:http://mvnq.baihongyu.com/

你可能感兴趣的文章
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>