Lyf凤

靡不有初,鲜克有终

扩大
缩小

第一类对象 闭包 迭代器

昨日回顾

函数的定义
        对功能或者动作的封装.
        语法:
            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

 

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

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

 

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

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

func1()
结果:
alex

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

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

func1()

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

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.那为什么我们可以称他们为可迭代对象呢?因为他们都遵循了可迭代协议.什么是可迭代协议.首先我们先看一段错误代码:

#对的
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函数来查看类中定义好的所有方法.

s =“我的哈哈哈”
print(dir(s))  #可以打印对象中的方法和函数
print(dir(str))#也可以打印类中声明的方法和函数

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

['__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将结束循环.

今日作业

作业

posted on 2019-07-26 11:18  Lyf凤  阅读(267)  评论(0编辑  收藏  举报

导航

levels of contents