第一类对象 闭包 迭代器

昨日回顾

函数的定义
        对功能或者动做的封装.
        语法:
            def 函数名(形参):
                函数体(return)

            函数名(实参)

        return:
            返回. 谁调用就返回到哪里
            1. 函数中不写return, 返回None.
            2, 只写了return, 也是返回None. 结束一个函数的运行
            3, return 值 返回一个值
            4, return 值1, 值2, 值3.... 一次返回多个结果. 打包成元组返回


        参数
            1.形参: 在函数声明的位置写的变量
                1. 位置参数
                2. 默认值参数
                3. 动态传参
                    *   聚合. 接收位置参数的动态传参
                    **  聚合. 接收关键字参数的动态传参
                参数列表顺序:
                    位置 > *args > 默认值 > **kwargs

            2.实参: 函数调用的地方写的具体的值
                1. 位置参数
                2. 关键字参数
                3. 混合参数:  位置 > 关键字
                    *  把列表, 字符串, 元组打散成位置参数(实参传入多个列表的话,每一个列表的前面都要加*)
                    ** 把字典打散成关键字参数
        名称空间:
            1. 内置名称空间
            2. 全局名称空间
            3. 局部名称空间

        做用域:
            1. 全局做用域: 全局名称空间 + 内置名称空间
            2. 局部做用域: 局部名称空间

        globals()  查看全局做用域中的名字,返回的类型是字典
        locals()   查看当前做用域中的名字,返回的类型是字典

        global   在局部引入全局做用域中的内容(全局不存在,帮全局建立)(只要引入了,引入那一层以后用的都是引用的,引用那一层修改了,全局的也会变)
        nonlocal 在局部, 在内层函数中引入离他最近的那一层局部做用域中的变量
View Code

 

今日内容

第一类对象-> 函数名 -> 变量名
        函数对象能够像变量同样进行赋值
        还能够做为列表的元素进行使用
        能够做为返回值返回
        能够做为参数进行传递

    闭包 -> 函数的嵌套
        内层函数对外层函数中的变量的使用

        好处:
            1. 保护变量不被侵害(#函数内部的变量均可以,由于在函数外部是没法调用函数内部的)
            2. 让一个变量常驻内存(#变量被使用就不会被垃圾回收机制回收)

        如何经过代码查看一个闭包
        函数名.__closure__: 有东西就是闭包. 没东西就不是闭包

    迭代器 -> 固定的思路. for循环
        一个数据类型中包含了__iter__函数表示这个数据是可迭代的
        dir(数据): 返回这个数据能够执行的全部操做

        判断迭代器和可迭代对象的方案(野路子)
        __iter__            可迭代的
        __iter__  __next__  迭代器

        判断迭代器和可迭代对象的方案(官方)
        from collections import Iterable, Iterator
        isinstance(对象, Iterable) 是不是可迭代的
        isinstance(对象, Iterator) 是不是迭代器

        模拟for循环
        lst= ['a','b','c']
        # 拿迭代器
        it = lst.__iter__()
        while 1:
            try:
                it.__next__()
            except StopIteration:
                break

        特征:
            1. 省内存(生成器)
            2. 惰性机制
            3. 只能向前. 不能后退

        做用:统一了各类数据类型的遍历

一.函数名的使用以及第-类对象

def fn():
    print("我叫fn")

fn()
print(fn)  # <function fn at 0x0000000001D12E18>
fn()
gn = fn # 函数名能够进行赋值
print(gn)
gn()

fn = 666
print(fn) # 666





def func1():
    print("朱祁镇")

def func2():
    print("徐阶")

def func3():
    print("王阳明")

def func4():
    print("魏忠贤")

lst = [func1, func2, func3, func4] # 函数+() 就是调用.
print(lst)  # [<function func1 at 0x1031cee18>, <function func2 at 0x103615f28>, <function func3 at 0x1119c6d90>, <function func4 at 0x1119c6e18>]


# lst[0]()
# for el in lst: #  el是列表中的每一项.
#     el() # 拿到函数. 执行函数



lst = [func1(), func2(), func3(), func4()]  # 存储函数调用以后的结果
print(lst)  # [None, None, None, None]结果都是None,由于没有return

# 结果:
# [<function func1 at 0x1031cee18>, <function func2 at 0x103615f28>, <function func3 at 0x1119c6d90>, <function func4 at 0x1119c6e18>]
# 朱祁镇
# 徐阶
# 王阳明
# 魏忠贤
# [None, None, None, None]






a = 10
b = 20
c = 30
lst = [a, b, c]
print(lst)


def wrapper():
    def inner():
        print("个人天, 还能够扎样写")
    print(inner) # <function wrapper.<locals>.inner at 0x00000000028989D8>
    inner()
    return inner

ret = wrapper() # <function wrapper.<locals>.inner at 0x00000000028989D8>
print(ret)
ret()

def wrapper():
    def inner():
        print("哈哈哈")
    return inner  # 函数名能够像返回值同样返回

ret = wrapper()
ret() # 在函数外面访问了函数内部的函数
ret()
ret()


def func1():
    a = 10
    return a
print(func1())




# 函数能够做为参数进行传递

def func1():
    print("谢晋")

def func2():
    print('杨士奇')

def func3():
    print('徐渭')

def func4():
    print("柳如是")

# 代理. 装饰器的雏形
def proxy(a): # a就是变量. 形参
    print("我是代理")
    a()
    print("代理执行完毕")

proxy(func1)
proxy(func3)
proxy(func4)
View Code

 

函数名是一个变量, 但它是-一个特殊的变量,与括号配合能够执行函数的变量.html

1.函数名的内存地址

def func():
    print("呵呵")

print(func)

结果:
<function func at 0x1101e4ea0>

2.函数名能够赋值给其余变量

def func():
    print("呵呵")

print(func)

a = func  #把函数当成一个变量赋值给另外一个变量
a()  #函数调用func()

3.函数名能够当作容器类的元素

def func1():
    print("呵呵")

def func2():
    print("呵呵")

def func3():
    print("呵呵")

def func4():
    print("呵呵")

lst = [func1, func2, func3]
for i in lst:
    i()

4.函数名能够当作函数的参数

def func():
    print("吃了么")

def func2(fn):
    print("我是func2")
    fn()#执行传递过来的fn
    print("我是func2")

func2(func)#把函数func当成参数传递给func2的参数fn.

5.函数名能够做为函数的返回值

def func_1():
    print("这里是函数1")
    def func_2(): 
        print("这里是函数2")
    print("这里是函数1")
    return func_2

fn = func_1() #执行函数1. 函数1返回的是函数2,这时fn指向的就是上面函数2
fn() # 执行上面返回的函数    

 

二.闭包

# 闭包的优势:
#   1, 能够保护变量不被其余人侵害
#   2, 保持一个变量常驻内存

def wrapper():
    a = "哈哈" # 不安全的一种写法
    name = "周杰伦"
    def inner():
        print(name) # 在内层函数中使用了外层函数的局部变量
        print(a)
    def ok():
        nonlocal a
        a = 108
        print(a)
    return inner  # 返回函数名

ret = wrapper()
ret()

def ok():
    global a  # 新建a
    a = 20
    print(a )



def wrapper():  # 记忆这个闭包的写法
    name = "周杰伦" # 局部变量常驻于内存
    def inner():
        print(name) # 在内层函数中使用了外层函数的局部变量
    return inner  # 返回函数名
    # inner()

ret = wrapper() # ret是一个内层函数
ret() # ret是inner, 执行的时机是不肯定的, 必须保证里面的name必须存在



# 超级简易版爬虫
from urllib.request import urlopen # 导入一个模块
# 干掉数字签名
import ssl
ssl._create_default_https_context = ssl._create_unverified_context


def func():
    # 获取到网页中的内容, 当网速很慢的时候. 反复的去打开这个网站. 很慢
    content = urlopen("https://www.dytt8.net/").read()

    def inner():
        return content.decode("gbk") # 网页内容
    return inner

print("开始网络请求")
ret = func() # 网络请求已经完毕
print("网络请求完毕")
print("第一次", ret()[5])
print("第二次", ret()[5]) #



def wrapper():
    name = "alex"
    def inner():
        print("胡辣汤")
    print(inner.__closure__) # 查看是不是闭包. 有内容就是闭包, 没有内容就不是闭包
    inner()

wrapper()
View Code

 

什么是闭包?闭包就是内层函数,对外层函数(非全局)的变量的引用.叫闭包python

def func1():
    name = "alex"
    def func2():
        print(name)#闭包
    func2()

func1()
结果:
alex

咱们可使用_ closure_ 来检测函数是不是闭包.使用函数名_ closure_ 返回cell就是闭包.返回None就不是闭包git

def func1():
    name = "alex"
    def func2():
        print( name )#闭包
    func2()
    print(func2.__ .closure__ )# (<cell at 0x10c2e20a8: str object at 0x10c3fc650> ,)

func1()

问题,如何在函数外边调用内部函数呢?api

def outer():
    name = "alex"
    #内部函数
    def inner():
        print(name)
    return inner

fn = outer() #访问外部函数,获取到内部函数的函数地址
fn()#访问内部函数

那若是多层嵌套呢?很简单,只须要一层一层的往外层返回就好了安全

def func1():
    def func2():
        def func3():
            print("嘿嘿")
        return func3
    return func2

func1()()()

由它咱们能够引出闭包的好处.因为咱们在外界能够访问内部函数.那这个时候内部函数访问的时间和时机就不必定了,由于在外部,我能够选择在任意的时间去访问内部函数.这个时候.想-想.咱们以前说过,若是一个函数执行完毕.则这个函数中的变量以及局部命名空间中的内容都将会被销毁.在闭包中.若是变量被销毁了.那内部函数将不能正常执行.因此python规定.若是你在内部函数中访问了外层函数中的变量.那么这个变量将不会消亡将会常驻在内存中.也就是说使用闭包,能够保证外层函数中的变量在内存中常驻.这样作有什么好处呢?很是大的好处.咱们来看一个关于爬虫的代码:网络

from urllib. request import urlopen

def but():
    content = urlopen("http://www .xiaohua100. cn/ index . html").read()
    def get_ contentO):
        return content
    return get_ content

fn = but() # 这个时候就开始加载校花100的内容
#后面须要用到这里面的内容就不须要在执行很是耗时的网络链接操做了
content = fn() # 获取内容
print(content)
content2 = fn() # 从新获取内容
print(content2)

综上,闭包的做用就是让一个变量可以常驻内存.供后面的程序使用.闭包

 

三.迭代器

s = "今天下午考试. 大家还要睡觉吗?"

for c in s: # 字符串是一个可迭代对象
    print(c)

for c in "哼哼哈哈":
    print(c)

for i in 123: # 'int' object is not iterable
    print(i)


# dir() 能够帮咱们查看xxx数据可以执行的操做
print(dir(str)) # __iter__
print(dir(int)) # 没有__iter__
print(dir(list)) # 有__iter__
print(dir(dict)) # 有__iter__
print(dir(bool)) # 没有__iter__
for i in True: # 'bool' object is not iterable
    print(i)

print(dir(range))
f = open("呵呵", mode="r", encoding="utf-8")
print(dir(f))

# 共性:全部带有__iter__的东西均可以进行for循环, 带有__iter__的东西就是可迭代对象

lst = ["贾樟柯", "李安", "杨德昌", "王家卫"]

# 1. 只能向下执行, 不能反复
# 2. 结束的时候会给咱们扔出来一个错误 StopIteration
# 3. 整合全部的数据类型进行遍历(int,bool除外)

# print("__iter__" in dir(lst))
it = lst.__iter__()  # 拿到的是迭代器 <list_iterator object at 0x0000000001DCC160>

print(it.__next__()) # 下一个
print(it.__next__()) # 下一个
print(it.__next__()) # 下一个
print(it.__next__()) # 下一个
# print(it.__next__()) # 下一个 # StopIteration 中止迭代
# 想回去
it = lst.__iter__() # 只能从新获取迭代器

s = {"张无忌", "贾樟柯", "宝宝", "风扇哥", "门神"}
it = s.__iter__()
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())
print(it.__next__())


lst = ["海尔兄弟", "阿童木", "葫芦娃", "舒克贝塔", "大风车"]
# 模拟for循环 for el in lst:
it = lst.__iter__() # 获取到迭代器0
while 1: # 循环
    try: # 尝试
        el = it.__next__() # 那数据
        print(el)
    except StopIteration: # 出了错误, 意味着数据拿完了
        break # 结束循环


# 官方经过代码判断是不是迭代器
# 借助于两个模块 Iterator迭代器, Iterable可迭代的
from collections import Iterable, Iterator

lst = [1,2,3]
# print(lst.__next__())

print(isinstance(lst, Iterable)) # xxx是不是xxx类型的. True
print(isinstance(lst, Iterator)) # False

it = lst.__iter__()  # 迭代器必定可迭代,  可迭代的东西不必定是迭代器

print(isinstance(it, Iterable)) # xxx是不是xxx类型的. True
print(isinstance(it, Iterator)) # True

for el in it:
    print(el)
View Code

 

咱们以前一直在用可迭代对象进行迭代操做.那么到底什么是可迭代对象.本小节主要讨论可迭代对象.首先咱们先回顾一下目前咱们所熟知的可迭代对象有哪些:
str, list, tuple, dict, set.那为何咱们能够称他们为可迭代对象呢?由于他们都遵循了可迭代协议.什么是可迭代协议.首先咱们先看一段错误代码:app

#对的
s="abc"
for c in s:
    print(c)


#错的
for i in 123:
    print(i)


结果:
Traceback (most recent call last):
File "/Users/sylar/PychamProjects/oldboy/iterator.py", line 8,in
<module>
for i in 123:
TypeError: 'int' object is not iterable

注意看报错信息中有这样一句话. 'int' object is not iterable .翻译过来就是整数类型对象是不可迭代的. iterable表示可迭代的.表示可迭代协议.那么如何进行验证你的数据类型是否符合可迭代协议.咱们能够经过dir函数来查看类中定义好的全部方法.ssh

s =“个人哈哈哈”
print(dir(s))  #能够打印对象中的方法和函数
print(dir(str))#也能够打印类中声明的方法和函数

在打印结果中.寻找__iter__ 若是能找到. 那么这个类的对象就是一个可迭代对象.ide

['__add__', '__class__', '__contains__', '__delattr__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__',
'__init_subclass__', __iter__ , '__le__', '__len__', '__lt__', '__mod__',
 '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode',
'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index',
'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower',
'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',
'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind',
'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines',
'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

咱们发如今字符串中能够找到__iter__. 继续看一下list, tuple, dict, set

print(dir(tuple))
print(dir(list)) 
print(dir(open("护⼠士少妇嫩模.txt"))) # 文件对象 
print(dir(set))
print(dir(dict))



# 结果
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', 
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',
'__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', 'count', 'index'] ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__',
'__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__',
'__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index',
'insert', 'pop', 'remove', 'reverse', 'sort'] ['_CHUNK_SIZE', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__',
'__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__',
'__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '_checkClosed', '_checkReadable', '_checkSeekable', '_checkWritable', '_finalizing', 'buffer', 'close', 'closed',
'detach', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'line_buffering', 'mode', 'name', 'newlines', 'read', 'readable', 'readline',
'readlines', 'seek', 'seekable', 'tell', 'truncate', 'writable', 'write', 'writelines'] ['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__',
'__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__',
'__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update',
'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference',
'symmetric_difference_update', 'union', 'update'] ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy',
'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

咱们发现这几个能够进行for循环的东西都有_ iter_ 函数, 包括range也有.能够本身试一下.


这是查看一个对象是不是可迭代对象的第一种办法咱们还能够经过isinstence0函数来查看一个对象是什么类型的

l = [1,2,3]
l_iter = 1.__ iter__()
from collections import Iterable
from collections import Iterator
print(isinstance(l , Iterable))  #True
print(isinstance(l , Iterator))  #False 
print(isinstance(l_ iter , Iterator))#True
print(isinstance(l_ iter , Iterable))#True

综上.咱们能够肯定.若是对象中有__iter__函数. 那么咱们认为这个对象遵照了可迭代协议.就能够获取到相应的选代器.这里的__iter__是 帮助咱们获取到对象的迭代器.
咱们使用迭代器中的__next__()来 获取到一个迭代器中的元素.

那么咱们以前讲的for的工做原理究竟是什么?继续看代码

s ="我爱北京天安门”
c = s.__iter__() #获取迭代器

print(c.__next__()) #使用迭代器进行迭代。获取一个元素我
print(c.__next__()) #
print(c.__next__()) #
print(c.__next__()) #
print(c.__next__()) #
print(c.__next__()) #
print(c.__next__()) #
print(c.__next__()) #StopIteration

for循环的机制:

for i in [1,2,3]:
    print(i)

使用while循环+迭代器来模拟for循环(必需要掌握)

lst = [1,2,3]
lst_ iter = lst.__iter__ ()
while True:
    try:
        i = lst. iter.__ .next___ .(O)
        print(i)
    except StopIteration:
        break

总结:
lterable:可迭代对象.内部包含__iter__()函数
Iterator:迭代器.内部包含__iter__() 同时包含__next__().
迭代器的特色:
  1.节省内存.
  2.惰性机制
  3.不能反复,只能向下执行.
咱们能够把要迭代的内容当成子弹.而后呢.获取到迭代器__iter__ (), 就把子弹都装在弹夹中而后发射就是__ next__ ()把每一 个 子弹(元素)打出来.也就是说, for循环的时候,一开始的时候是__iter__.()来 获取迭代器.后面每次获取元素都是经过__ next__ .()来完成的. 当程序遇到Stoplteration将结束循环.

今日做业

做业

相关文章
相关标签/搜索