zoukankan      html  css  js  c++  java
  • 闭包函数

    # 闭包函数:定义在函数内部的函数,该函数的函数体代码包含对外部作用域(而不是全局作用域)名字的引用
    # def outer():
    # x=1
    # def inner():
    # print(x)
    # return inner
    #
    # f=outer()
    # print(f) # <function outer.<locals>.inner at 0x000001F6C8F74C18>
    # print(f.__closure__) # __closure__ 闭合 (<cell at 0x000001605D38A078: int object at 0x00007FFD2F717110>,)
    # print(f.__closure__[0]) # <cell at 0x0000021A6471A078: int object at 0x00007FFD2F717110>
    # print(f.__closure__[0].cell_contents) # 1

    # def outer():
    # x=1
    # y=2
    # def inner():
    # print(x,y)
    # return inner
    #
    # f=outer()
    # print(f.__closure__[0].cell_contents) # 1
    # print(f.__closure__[1].cell_contents) # 2

    # z=3
    # def outer():
    # x=1
    # y=2
    # def inner():
    # print(z)
    # return inner
    #
    # f=outer()
    # print(f.__closure__) # None

    # 闭包函数:
    # 1、定义在函数内部的函数
    # 2、该函数的函数体代码包含对外部作用域(而不是全局作用域)名字的引用
    # 3、通常将闭包函数用return返回,可以在任意位置使用

    # 爬页面:
    # import requests # pip3 install requests # 下载命令
    # 模仿浏览器,发送http协议的请求,就是一个浏览器,发送请求给服务端,服务端把网页文件的内容返回给你
    # # 区别在于浏览器下载下来以后会显示各种各样的样式,但是它下载下来什么就是什么
    # response=requests.get('http://www.baidu.com')
    # print(response.text)
    # print(len(response.text)) # 2381
    # import requests
    # response=requests.get('http://www.baidu.com')
    # if response.status_code == 200: # 对方服务返回的响应结果的状态,如果是200,表示成功连接上对方的服务器
    # print(len(response.text)) # 2381
    # import requests
    # def get(url):
    # response=requests.get(url)
    # if response.status_code == 200:
    # print(len(response.text))
    #
    # get('http://www.baidu.com') # 2381

    # import requests
    # def outter(url):
    # def get():
    # response=requests.get(url)
    # if response.status_code == 200:
    # print(len(response.text))
    # return get
    #
    # get=outter('http://www.baidu.com')
    # get() # 2381
  • 相关阅读:
    Windows Phone MultiBinding :Cimbalino Toolkit
    Instant Django 1.5 Application Development Starter
    Writing your first Django
    Python urllib2 proxy
    TED Notes 1 (What leads to success)
    Py2.7 no module named tkinter
    某培训的笔记
    “假学习”和“真学习”[转]
    Selenium学习笔记
    反思之一
  • 原文地址:https://www.cnblogs.com/0B0S/p/11972871.html
Copyright © 2011-2022 走看看