为什么执行结果是这样的???
怎么会是连续两个”日志记录!”
def outfunc(func): def infunc(): func1() logindata(func) return infunc
老师我想问下,这个闭包的外部函数不是传进了参数func,为啥不用把这个参数传给内部函数,可他却能直接给内部函数里的方法使用
老师我标记处的两种写法有没有什么区别呢?
time (func): (*args**kwargs): start = time.time() result = func(*args**kwargs) end = time.time() (end-start) result infunc CaheDecorator(): __cache={} (func): .func = func (*args**kwargs): .func.CaheDecorator.__cache: CaheDecorator.__cache[.func.] : result = .func(*args**kwargs) CaheDecorator.__cache[.func.] = result (.__cache) result (): () time.sleep() () __name__ == : r1 = func1_long_time() r2 = func1_long_time() (r1) (r2)
这段代码他的运行顺序是怎么样的?
#时间模块 import time #sys系统模块 import sys time.clock() list1 = [x for x in range(2,10001,2)] costTime = time.clock() print('创建列表耗时:%g'%costTime)
老师,您好。我的代码是在按照视频上敲的,但有个问题,如下图,可怎么解决呀?
老师请问一下是因为最终log里面return了with_logging所以test.__name__打印的是with_logging的名字,可以这样去理解吗?谢谢老师
老师,为什么在这里会设有三个返回值呢,设置返回值的意义是什么啊,不是太清楚
老师,这个方法,只限定了最小必须匹配8位,如果输入20位以上,不会报None
#encoding=utf-8 import time def cost_time(func): def infunc(*args,**kwargs): start = time.time() result = func(*args,**kwargs) end = time.time() print(f"耗时:{end-start}") return result return infunc class CaheDecorator(): __cache={} #类属性,用作所有实例的共享缓存 def __init__ (self ,func): self.func = func def __call__ (self, *args, **kwargs): #如果缓存中有对应的方法名,则直接返回对应的返回值 if self.func.__name__ in CaheDecorator.__cache: return CaheDecorator.__cache[self.func.__name__] # 如果缓存中没有对应的方法名,则进行计算,并将结果缓存 else: result = self.func(*args,**kwargs) CaheDecorator.__cache[self.func.__name__] = result print(self.__cache) return result @cost_time #func1_long_time = cost_time(func1_long_time) @CaheDecorator #func1_long_time=CaheDecorator(func1_long_time) def func1_long_time(): """模拟耗时较长,每次执行返回结果都一样的情况""" print("start func1") time.sleep(3) print("end func1") return 999 if __name__ == '__main__': r1 = func1_long_time() r2 = func1_long_time() print(r1) print(r2)
这段代码的运行顺序是怎么样的?
reduce和map的区别在哪,两个好像都差不多
老师函数里面已经有a=10了,函数外面为什么还要加一个呢?我搞不明白,外面的有什么用
老师这段代码,在n=2时为啥不会打印呢??????
nonlocal a 在这里起什么作用 为什么要申明a 不是本地变量
正常情况的函数是不是变量用完就不能再用了,也就是说闭包的最大特点是变量用完还能用吗?
老师,str1 = re.sub(pattern,replace,s),中,为啥,会把s自动传给replace函数?是re.sub中定义的吗?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2026百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637