• Python从入门到放弃_函数闭包_迭代器


    # def func():

    # print(1)
    #
    # print(func) #内存地址 <function func at 0x027B34B0>

    # def func():
    # print(1)
    # a = func # 函数名当做值赋值给变量
    # a()
    # func()

    # def func():
    # # print(1)
    # return '大姑娘美'
    #
    # def func2(msg): # mag = func
    # print(msg) # msg??? func内存地址
    #
    # func2(func()) # 函数名可以当做参数去传递 注意点 传递函数名的时候如果+()就是在传递这个函数的返回值

    # def foo():
    # print(2)
    # # 当函数没有写return默认返回None
    # def func(msg):
    # print(1)
    #
    # return msg
    # print(func(foo)) # 函数名可以当做返回值被返回

    # def foo():
    # print(2)
    # # 当函数没有写return默认返回None
    # def func(msg):
    # print(1)
    #
    # return msg() # 你们每天都在犯的一个问题
    # print(func(foo)) # 函数名可以当做返回值被返回


    # def foo():
    # print(1)
    # return '我是策哥'
    #
    # def func():
    # print(2)
    # print(foo()) # foo() = '我是策哥'
    # return '我是军哥'
    #
    # def func2():
    # print(3)
    # print(func())
    # return '我是强哥'
    #
    # print(func2()) # func2() = '我是强哥'


    # def foo():
    # print(1)
    # return '我是策哥'
    #
    # def func():
    # print(2)
    # ret = foo() # foo() = '我是策哥'
    # return ret
    #
    # def func2():
    # print(3)
    # msg = func()
    # return msg

    # print(func2())

    # lst = [foo,func,func2] # 函数名可以当做元素存放在一个容器当中
    # print(lst)
    #
    # for i in lst: # 遍历 for循环
    # i()


    # 总结:
    # 函数名可以 当做值 去赋值给变量
    # 函数名可以 当做参数传递给函数
    # 函数名可以 当做函数返回值,注意:返回值不能加括号 接力的要一层一层往下传,中间断了就接不到了
    # 函数名可以 当做元素存放一个容器里
    #  查看函数的内存地址 print(函数名)


    # def foo():
    # print('foo很痛恨func')
    # return '接水'
    #
    # def func():
    # print('让foo去接水')
    # ret = foo()
    # return ret + '加了点药'
    #
    # def func2():
    # print('让func去接水')
    # func()
    #
    # func2()

    # 闭包

    # def func():
    # name = '宝元'
    # print(name)
    # func() #函数 消失了


    # def func():
    # def foo():
    # name = '宝元'
    # print(name)
    # foo()
    # print(foo.__closure__)
    # func()

    # def func():
    # name = '宝元'
    # def foo():
    # print(name)
    # foo()
    # # print(foo.__closure__) # (<cell at 0x007C1370: str object at 0x007C45C0>,)
    # func()

    # print(func.__closure__) # None

    # 闭包:1.嵌套函数,内部函数使用外部变量(非全局变量)就是闭包


    # 使用函数.__closure__ 获取的内容不是None


    # def func():
    # name = '宝元' # 生命周期延长 常驻内存
    # def foo():
    # print(name)
    # return foo
    # ret = func() # ret == foo内存地址
    # ret() # foo函数


    # 好处,
    # 保护这个变量不被外界修改
    # 生命周期延长
    # 节省开辟空间和销毁空间的时间差,提高效率

    # def func(): #大门
    # name = '宝元' # 生命周期延长 常驻内存
    # def foo(): # 班门
    # print(name)
    # return foo

    # ret = func()
    # ret()
    # func()() #func() == foo foo() func()()
    # ret = func()
    # ret()
    # func()()

    # def func():
    # n = 10
    # def f1():
    # print(n)
    # return f1
    # func()()

    # 应用场景

    import requests
    # ret = requests.get('http://www.xiaohuar.com/')
    # print(ret.text)

    # def get_url():
    # url ='666'
    # def get():
    # s = 4
    # print(url)
    # def urls():
    # print(s)
    # return urls
    # return get
    # msg = get_url()
    # msg()()


    # urls = get()
    # print(get()())
    # 装饰器 回顾闭包


    # 迭代器 在原来额基础上更新???

    # 遵守迭代协议(标准):
    # 只要有__iter__方法的就是可迭代对象
    # 可以被for循环的就是可迭代对象

    # print(dir(str)) # 查看这个类型中有什么功能 可迭代__iter__
    # list
    # dict
    # set
    # tuple

    # print(s.__iter__()) # 对象 iterator 迭代器
    # print(lst.__iter__())
    # print(dict.__iter__({'1':2}))
    # print(set.__iter__({'1',2}))
    # print(tuple.__iter__(('1',2)))
    # print(range.__iter__(range(0,7)))
    # print(dir(int))
    # print(dir(bool))


    # print(s.__next__())
    # print(s.__next__())
    # s.__iter__() # 创建了一个迭代器 具有__iter__方法和__next__方法
    # print(s.__next__())
    # print(s.__next__())
    # print(s.__next__())

    # 迭代器不能回退
    # 迭代器是惰性的
    # 迭代器是一次性的

    # lst = [1,2,3,4,5,6]
    #
    # l = lst.__iter__() # 从一个可跌代对象转换成迭代器
    # print(l.__next__())
    # print(l.__next__())
    # print(l.__next__())
    # print(l.__next__())
    # print(l.__next__())
    # print(l.__next__())
    # print(l.__next__())

    # lit = [1,2,3,4,5,6]
    #
    # for i in lit:
    # print(i)

    ##########for循环的机制###########

    # lst = [1,2,3,4,5]
    # count = 0
    # l = lst.__iter__()
    # while count < len(lst):
    # print(l.__next__())
    # count += 1


    # lst = [1,2,3,4,5,67,7]
    # l = lst.__iter__()
    # while True:
    # try: # 异常捕获
    # print(l.__next__())
    # except StopIteration:
    # break

    # s = '123'.__iter__()
    # s1 = s.__iter__()
    #
    # print(s1.__next__())
    # print(s.__next__())

    # from collections import Iterable,Iterator
    #
    # print(isinstance([1,2,4].__iter__(),Iterator))
    # print(isinstance([1,2,4],Iterable))
    #
    # print(isinstance('123',Iterator))
    # print(isinstance('123',Iterable))
    #
    # print(isinstance(123,Iterator))
    # print(isinstance(123,Iterable))

  • 相关阅读:
    Nginx与Apache的对比
    gc buffer busy waits(ZT)
    Brocade SAN Switch Change Domain ID (ZT)
    Oracle异机恢复时报错ora19870 ora19507
    row cache lock (ZT)
    can a select block a truncate (ZT)
    NBU常用命令
    the RRD does not contain an RRA matching the chosen C
    Solaris10 x64安装64bit perl
    Solaris and Oracle 32bit Linking Error "fatal: symbol 'ntcontab'
  • 原文地址:https://www.cnblogs.com/fp233/p/10273984.html
Copyright © 2020-2023  润新知