简单的整理了下装饰器、生成器、迭代器
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: linghanchujianimport time# 装饰器def fun(function): def funs(*args,**kwargs): Start_Time = time.time() res=function(*args,*kwargs) End_Time = time.time() print("run fun time is %s"%(End_Time-Start_Time)) return res return funs passdef func(): time.sleep(0.5) print("the is func!!") pass@fun # 相当于 func1 = fun(func1)def func1(name): func() time.sleep(0.5) print("the is func1!!",name) return 100 passprint(func1("Linghanchujian")) #实际上执行的是funs()这个函数# 装饰器def func3(isFunc): def func4(Func): def func5(*args,**kwargs): if isFunc: print("isFunc is True!!") res = Func(*args, **kwargs) else: print("isFunc is False!!") res = Func(*args, **kwargs) return res pass return func5 pass return func4 pass@func3(isFunc=True)def func(Name): print("the is func!! My name is %s"%Name) pass@func3(isFunc=False)def func1(Name,age): print("the is func1!! My name is %s and age is %s"%(Name,age)) passfunc("Linghanchujian")func1("Linghanchujian",22)# 列表生成器array = [i*2 for i in range(10)] # 会一次性生成所有列表数据 可以通过 array[i] 来取值print(array[8])array1 = (i*2 for i in range(10)) # 不会生成数据,只会创建地址保存算式 可以通过 __next__() 一个个取值 但是一般是用 for infor j,k in enumerate(array1): if j==8: print(k) break # 直接打印def SeriseOne(Max): i,a,b,c = 0,0,1,0 while i