python-三器一闭

1.迭代器

1.自定义迭代器

 1 class MyList(object):
 2     # 自定义迭代器对象
 3     def __init__(self):
 4         self.current = 0
 5         self.container = [1, 2, 3, 4, 9, 8, 7, 6, 5]
 6 
 7     def __iter__(self):
 8         # 1.标记当前类创建出的对象为可迭代对象
 9         # 2.当调用iter()时,返回指定迭代器
10         return self
11 
12     def __next__(self):
13         # 1.标记当前类创建出的对象为可迭代器
14         # 2.当调用next()时,返回指定数据
15         if self.current < len(self.container):
16             self.current += 1
17             return self.container[self.current - 1]
18         else:
19             self.current = 0
20             raise StopIteration

2.生成器

生成不确定个数的数据,使用生成器.

1.生成器一

1 # 列表生成式
2 nums = [x for x in range(10000000)]
3 print(nums)  # 一次性全部生成 
4 # 生成器
5 nums = (x for x in range(10000000))
6 for i in nums:
7     print(i)  # 使用时生成

2.生成器二

 1 class MyList(object):
 2     # 自定义迭代器对象
 3     def __init__(self):
 4         self.current = 0
 5         self.container = 0
 6 
 7     def __iter__(self):
 8         # 1.标记当前类创建出的对象为可迭代对象
 9         # 2.当调用iter()时,返回指定迭代器
10         return self
11 
12     def __next__(self):
13         # 1.标记当前类创建出的对象为可迭代器
14         # 2.当调用next()时,返回指定数据
15         self.container += 2
16         return self.container - 1
17 
18 
19 c = MyList()
20 for i in range(10):
21     print(next(c))

3.生成器三

 1 def fib_gen():
 2     num = 0
 3     while True:
 4         num += 2
 5         yield num
 6 
 7 # fib为生成器
 8 fib = fib_gen()
 9 for i in range(10):
10     print(next(fib))

4.生成器四

 1 def fib_gen():
 2     num = 0
 3     while True:
 4         num += yield 1
 5         print("n" + str(num))
 6 
 7 
 8 fib = fib_gen()
 9 next(fib)  # 执行到yield停止 可以使用fib.send(None)代替
10 for i in range(10):
11     fib.send(2)  # 将2传回yield位置并赋值

5.yield from

 1 def gen():
 2     for c in 'AB':
 3         yield c
 4     for i in range(1, 3):
 5         yield i
 6 
 7 # 等价于
 8 
 9 def gen():
10     yield from 'AB'
11     yield from range(1, 3)

3.闭包

定义外部函数和内部函数,外部函数返回内部函数的引用,内部函数使用了外部函数的变量.

1.简单闭包

1 def outer(name1):
2     def inner(name2):
3         print(name1 + "" + name2 + "")
4      nonlocal name1 
5     return inner
6 
7 
8 a = outer('张三')
9 a('李四')

闭包会让参数和变量不被垃圾回收机制回收,不再使用是应手动删除

4.装饰器

通用装饰器

 1 def outer(func):
 2     def inner(*args, **kwargs):
 3         return func(*args, **kwargs)
 4     return inner
 5 
 6 
 7 @outer
 8 def f1():
 9     print('f1')
10 f1()
11 # 等价于
12 f2 = outer(f1)
13 f2()

多个装饰器装饰同一函数,就近原则.

含参装饰器

 1 import time
 2 
 3 
 4 def outer(timeout=0):
 5     def inner1(func):
 6         def inner2(*args, **kwargs):
 7             time.sleep(timeout)
 8             return func(*args, **kwargs)
 9 
10         return inner2
11 
12     return inner1
13 
14 
15 @outer(2)
16 def f1():
17     print('f1')
18 
19 
20 f1()

类装饰器

 1 class Test(object):
 2     def __init__(self, func):
 3         print('1')
 4         self.__func = func
 5 
 6     def __call__(self):
 7         print('2')
 8         self.__func()
 9 
10 
11 @Test
12 def test():
13     print('3')
14 
15 
16 test()

含参类装饰器

 1 class Test(object):
 2     def __init__(self, timeout=0):
 3         print('1')
 4         self.timeout = timeout
 5 
 6     def __call__(self,func):
 7         print('2')
 8         self.__func = func
 9         return self.my_call
10 
11     def my_call(self):
12         print('3')
13         return self.__func()
14 
15 
16 @Test(2)
17 def test():
18     print('4')
19 
20 
21 test()

 

posted @ 2022-06-20 19:19  隐仙阁-x9  阅读(86)  评论(0)    收藏  举报