闭包迭代器

一、闭包就是内层函数对外层函数(非全局)的变量的应用。

 1.可以让被调用 的变量常驻内存

def func():
name = 'alex'
def inner():
print(name) #在内层函数中调用了外层函数的变量,叫闭包,可以让一个局部变量常驻内存
return inner

ret = func()
ret()

2.优点
  安全,常驻内存,提高效率
3.迭代器
  可迭代对象(Iterable):内部有__iter__()
  迭代器 (Iterator):内部有__iter__() __next__()
  str, list, tuple, set, dict, f句柄
  
  迭代器特点
    1.省内存
    2.惰性机制
    3.只能向前














原文地址:https://www.cnblogs.com/yb161/p/9828866.html