python13各种器
def hello(): print("hello") def test(): print("test") def hello_wrapper(): print("aaa") hello() print("bbbb") def test_wrapper(): print("aaaa") hello() print("bnbbb") if __name__ == "__main__": #hello() hello_wrapper() test_wrapper()
用装饰器进行改良:
def log(func): def wrapper(): print("aaaa") func() print("bbbb") return wrapper @log def hello(): print("hello") def log_in(func): def wrapper(): print("inaaaa") func() print("inbbbb") return wrapper @log #1开始进入装饰器,调用函数后,然后进入装饰器log_in @log_in #执行函数,退出装饰器log_in ,退出装饰器log def test(): print("test") if __name__ == "__main__": hello() test()
结果:
aaaa
hello
bbbb
aaaa
inaaaa
test
inbbbb
bbbb
#装饰器传入参数 def log(name=None): def decorator(func): def wrapper(): print("{}aaaa".format(name)) func() print("{}bbbb".format(name)) return wrapper return decorator @log() def hello(): print("hello") @log("test") def test(): print("test....") if __name__ == "__main__": hello() test() 结果: Noneaaaa hello Nonebbbb testaaaa test.... testbbbb
#装饰器传入参数 def log(name=None): def decorator(func): def wrapper(*args,**kwargs): print("{}aaaa".format(name)) rest = func(*args,**kwargs) print("{}bbbb".format(name)) print(rest) return wrapper return decorator @log() def add(a,b): return a + b @log("okok") def add2(a,b): return a + b if __name__ == "__main__": add(5,6) add2(5,6) 结果: Noneaaaa Nonebbbb 11 okokaaaa okokbbbb 11
#装饰器传入参数 from functools import wraps def log(name=None): def decorator(func): #@wraps(func)#加入这个参数,可以还原原来函数的属性。 #要不就要加入下面那两行,或者更多 def wrapper(*args,**kwargs): "装饰器的名字" print("{}aaaa".format(name)) rest = func(*args,**kwargs) print("{}bbbb".format(name)) return rest#原函数执行到这里完成后,属性改变 #wrapper.__name__ = func.__name__ #wrapper.__doc__ = func.__doc__ return wrapper return decorator @log("okok") def hello(): """ hello :return: """ print("hello") if __name__ == "__main__": print("doc:{}".format(hello.__doc__)) print("name:{}".format(hello.__name__)) hello() 结果: doc:装饰器的名字 name:wrapper okokaaaa hello okokbbbb
#装饰器传入参数 from functools import wraps def log(name=None): def decorator(func): @wraps(func)#加入这个参数,可以还原原来函数的属性。 #要不就要加入下面那两行,或者更多 def wrapper(*args,**kwargs): "装饰器的名字" print("{}aaaa".format(name)) rest = func(*args,**kwargs) print("{}bbbb".format(name)) return rest#原函数执行到这里完成后,属性改变 #wrapper.__name__ = func.__name__ #wrapper.__doc__ = func.__doc__ return wrapper return decorator @log("okok") def hello(): """ hello :return: """ print("hello") if __name__ == "__main__": print("doc:{}".format(hello.__doc__)) print("name:{}".format(hello.__name__)) hello() 结果: doc: hello :return: name:hello okokaaaa hello okokbbbb
两个是python中的可变参数。*args表示任何多个无名参数,它是一个tuple;**kwargs表示关键字参数,它是一个 dict。并且同时使用*args和**kwargs时,必须*args参数列要在**kwargs前,像foo(a=1, b=’2′, c=3, a’, 1, None, )
这样调用的话,会提示语法错误“SyntaxError: non-keyword arg after keyword arg”。
对于一些类,都需要添加一些属性或者方法,我们可以通过装饰器,取到一个类,然后进行添加
def eat(cls): """吃东西装饰器""" cls.eat = lambda self : print("{}要wowowo".format(self.name)) return cls @eat class Cat(object): """猫猫猫""" def __init__(self,name): self.name = name if __name__ == "__main__": cat =Cat("quan") cat.eat() 结果: quan要wowowo
class Power(object): """ 迭代器,生成1 2345的平方 """ value = 0 def __next__(self): self.value += 1 return self.value * self.value def __iter__(self): return self if __name__ == "__main__": Pos = Power() print(Pos.__next__()) print(Pos.__next__()) print(Pos.__next__()) print(Pos.__next__()) print(next(Pos)) for i in Pos: print(i) 结果: 1 4 9 16 25
def pow(): yield 1 yield 2 yield 3 yield 4 def pow_number(): for i in [1,2,3,4]: yield i * i if __name__ == "__main__": # rest = pow() # print(next(rest)) # print(next(rest)) # print(next(rest)) rest = pow_number() print(next(rest)) print(next(rest)) #生成器也是一种迭代器,
结果:
1 4
def use_range(): for i in range(5,10): print(i) class Iter(object): """使用迭代器模拟range""" def __init__(self,start,end): self.start = start - 1 self.end = end def __next__(self): self.start += 1 if self.start >= self.end: raise StopIteration return self.start def __iter__(self): return self if __name__ == "__main__": use_range() print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@") iter = Iter(5,10) print(next(iter)) print(next(iter)) print(next(iter)) print(next(iter)) print(next(iter)) iter2 = Iter(5,10) l = list(iter2)#也可以返回一个列表 print(l)
生成器模拟range()
def use_range(): for i in range(5,10): print(i) class Iter(object): """使用迭代器模拟range""" def __init__(self,start,end): self.start = start - 1 self.end = end def __next__(self): self.start += 1 if self.start >= self.end: raise StopIteration return self.start def __iter__(self): return self class Gen(object): """生成器模拟range函数""" def __init__(self,start,end): self.start = start - 1 self.end = end def get_num(self): while True: if self.start >= self.end - 1: break self.start +=1 yield self.start if __name__ == "__main__": use_range() print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@") iter = Iter(5,10) print(next(iter)) print(next(iter)) print(next(iter)) print(next(iter)) print(next(iter)) iter2 = Iter(5,10) l = list(iter2)#也可以返回一个列表 print(l) gen = Gen(5,10).get_num() print(list(gen))
5 6 7 8 9 @@@@@@@@@@@@@@@@@@@@@@@@@@@@ 5 6 7 8 9 [5, 6, 7, 8, 9] [5, 6, 7, 8, 9]
去掉类:
def gen_num(start,end): start -= 1 while True: if start >= end - 1: break start +=1 yield start if __name__ == "__main__": print(list(gen_num(5,10))) 结果: [5, 6, 7, 8, 9]