# 迭代器、装饰器、生成器 # 迭代器 li = [1, 3, 'he', '&'] n = iter(li) print(n.__next__()) import os, sys # 生成器 def func(): for i in xrange(10): yield (i) print(i)
1 # 装饰器 2 #使用装饰函数在函数执行前和执行后分别附加额外功能 3 '''示例2: 替换函数(装饰) 4 装饰函数的参数是被装饰的函数对象,返回原函数对象 5 装饰的实质语句: myfunc = deco(myfunc)''' 6 def deco(func): 7 print("before myfunc() called.") 8 func() 9 print(" after myfunc() called.") 10 return func 11 def myfunc(): 12 print(" myfunc() called.") 13 myfunc = deco(myfunc) 14 myfunc() 15 myfunc() 16 #使用语法糖@来装饰函数 17 '''示例3: 使用语法糖@来装饰函数,相当于“myfunc = deco(myfunc)” 18 但发现新函数只在第一次被调用,且原函数多调用了一次''' 19 def deco(func): 20 print("before myfunc() called.") 21 func() 22 print(" after myfunc() called.") 23 return func 24 @deco 25 def myfunc(): 26 print(" myfunc() called.") 27 myfunc() 28 myfunc() 29 30 #用内嵌包装函数来确保每次新函数都被调用 31 def deco(func): 32 def _deco(): 33 print("before myfunc() called.") 34 func() 35 print(" after myfunc() called.") 36 # 不需要返回func,实际上应返回原函数的返回值 37 return _deco 38 @deco 39 def myfunc(): 40 print(" myfunc() called.") 41 return 'ok' 42 myfunc() 43 myfunc() 44 #对带参数的函数进行装饰 45 def deco(func): 46 def _deco(a, b): 47 print("before myfunc() called.") 48 ret = func(a, b) 49 print(" after myfunc() called. result: %s" % ret) 50 return ret 51 return _deco 52 @deco 53 def myfunc(a, b): 54 print(" myfunc(%s,%s) called." % (a, b)) 55 return a + b 56 myfunc(1, 2) 57 myfunc(3, 4) 58 #对参数数量不确定的函数进行装饰 59 '''示例6: 对参数数量不确定的函数进行装饰, 60 参数用(*args, **kwargs),自动适应变参和命名参数''' 61 def deco(func): 62 def _deco(*args, **kwargs): 63 print("before %s called." % func.__name__) 64 ret = func(*args, **kwargs) 65 print(" after %s called. result: %s" % (func.__name__, ret)) 66 return ret 67 return _deco 68 @deco 69 def myfunc(a, b): 70 print(" myfunc(%s,%s) called." % (a, b)) 71 return a+b 72 @deco 73 def myfunc2(a, b, c): 74 print(" myfunc2(%s,%s,%s) called." % (a, b, c)) 75 return a+b+c 76 myfunc(1, 2) 77 myfunc(3, 4) 78 myfunc2(1, 2, 3) 79 myfunc2(3, 4, 5) 80 81 #让装饰器带参数 82 def deco(arg): 83 def _deco(func): 84 def __deco(): 85 print("before %s called [%s]." % (func.__name__, arg)) 86 func() 87 print(" after %s called [%s]." % (func.__name__, arg)) 88 return __deco 89 return _deco 90 91 @deco("mymodule") 92 def myfunc(): 93 print(" myfunc() called.") 94 95 @deco("module2") 96 97 def myfunc2(): 98 print(" myfunc2() called.") 99 myfunc() 100 myfunc2() 101 #让装饰器带 类 参数 102 '''示例8: 装饰器带类参数''' 103 class locker: 104 def __init__(self): 105 print("locker.__init__() should be not called.") 106 @staticmethod 107 def acquire(): 108 print("locker.acquire() called.(这是静态方法)") 109 @staticmethod 110 def release(): 111 print(" locker.release() called.(不需要对象实例)") 112 def deco(cls): 113 '''cls 必须实现acquire和release静态方法''' 114 def _deco(func): 115 def __deco(): 116 print("before %s called [%s]." % (func.__name__, cls)) 117 cls.acquire() 118 try: 119 return func() 120 finally: 121 cls.release() 122 return __deco 123 return _deco 124 @deco(locker) 125 def myfunc(): 126 print(" myfunc() called.") 127 myfunc() 128 myfunc() 129 #装饰器带类参数,并分拆公共类到其他py文件中,同时演示了对一个函数应用多个装饰器 130 '''mylocker.py: 公共类 for 示例9.py''' 131 class mylocker: 132 def __init__(self): 133 print("mylocker.__init__() called.") 134 @staticmethod 135 def acquire(): 136 print("mylocker.acquire() called.") 137 @staticmethod 138 def unlock(): 139 print(" mylocker.unlock() called.") 140 class lockerex(mylocker): 141 @staticmethod 142 def acquire(): 143 print("lockerex.acquire() called.") 144 @staticmethod 145 def unlock(): 146 print(" lockerex.unlock() called.") 147 def lockhelper(cls): 148 '''cls 必须实现acquire和release静态方法''' 149 def _deco(func): 150 def __deco(*args, **kwargs): 151 print("before %s called." % func.__name__) 152 cls.acquire() 153 try: 154 return func(*args, **kwargs) 155 finally: 156 cls.unlock() 157 return __deco 158 return _deco 159 '''示例9: 装饰器带类参数,并分拆公共类到其他py文件中 160 同时演示了对一个函数应用多个装饰器''' 161 from mylocker import * 162 class example: 163 @lockhelper(mylocker) 164 def myfunc(self): 165 print(" myfunc() called.") 166 @lockhelper(mylocker) 167 @lockhelper(lockerex) 168 def myfunc2(self, a, b): 169 print(" myfunc2() called.") 170 return a + b 171 if __name__=="__main__": 172 a = example() 173 a.myfunc() 174 print(a.myfunc()) 175 print(a.myfunc2(1, 2)) 176 print(a.myfunc2(3, 4))
1 # 递归 2 #斐波那契数列 3 def func(arg1, arg2, stop): 4 if arg1 == 0: 5 print(arg1, arg2) 6 arg3 = arg1 + arg2 7 print(arg3) 8 if arg3 < stop: 9 func(arg2, arg3, stop) 10 11 func(0, 1, 60)
1 #算法基础之二分查找 2 def data_search(data,find_d): 3 mid=int(len(data)/2) 4 if len(data)>=1: 5 if data[mid]>find_d: 6 print("data in thie left of [%s]"%data[mid]) 7 data_search(data[:mid],find_d) 8 elif data[mid]<find_d: 9 print("data in right of [%s]"%data[mid]) 10 data_search(data[mid:],find_d) 11 else: 12 print("found find_d",data[mid]) 13 else: 14 print("cannot find`````") 15 if __name__=="__main__": 16 data=list(range(1,60000000)) 17 data_search(data,99) 18 19 #二维数组旋转 20 data=[[col for col in range(4)]for row in range(4)] 21 print(data) 22 for rindex,row in enumerate(data): 23 print(rindex,row) 24 for cindex in range(rindex,len(row)): 25 tmp=data[cindex][rindex] 26 data[cindex][rindex]=row[cindex] 27 data[rindex][cindex]=tmp 28 print('-------------------------') 29 for r in data:print(r)