闭包

# 闭包:内部定义的函数,该函数包含对外部作用域而不是全局作用域名字的引用
# 内部定义的函数是对内部作用域的引用,而不是对外部作用域的引用
# 内部函数的代码是对函数内部的引用,例如x=1在函数里面,而不是在外面
# x=12345
# def f1():
#     x=1
#     def f2():
#         print(x)
#     return f2
#
# f=f1()
# f()
# print(f.__closure__)

# 上面结果(<cell at 0x000000000212FE58: int object at 0x000000001E016DE0>,),元组里只有一个说明函数内只有一个引用。
# 当我们增加一个引用时:元组就增加一个(<cell at 0x000000000248FE58: int object at 0x000000001DFC6DE0>, <cell at 0x0000000002893378: int object at 0x000000001DFC7360>)
# print(f.__closure__[1].cell_contents)是返回y的值
# x=12345
# def f1():
#     x=1
#     y=45
#     def f2():
#         print(x)
#         y
#     return f2
#
# f=f1()
# f()
# print(f.__closure__)

# 显示f.__closure__的内容:[0]和[1]是按照字母顺序来排的,例如把x=1,y=45改为x=1,b=2,[0]就是2而不是1
# print(f.__closure__[0].cell_contents)
# print(f.__closure__[1].cell_contents)

# 把函数内部的赋值删除,print(f.__closure__)结果就是None
# print(f.__closure__[1].cell_contents)是返回y的值
# x=12345
# def f1():
#     def f2():
#         print(x)
#     return f2
#
# f=f1()
# f()
# # print(f.__closure__)
# # print(f.__closure__[0].cell_contents)
#

# 下载pip install requests


 

安装pip

在计算机运行输入cmd

把拖动Python主应用程序到命令行窗口,直至命令行窗口增加了如下代码

在命令行窗口输入“空格”,然后拖动get-pip.py 脚本到命令行窗口。回车,这时候pip就会自动安装了

按照下面的语句输入,就可以安装pip了:

 

200是打开成功百度的意思,跟浏览器打开百度一样

 

输入下面命令:

 

跟在浏览器打开百度,右键选检查的代码是一样的

 

# 下载网页的信息
# from urllib.request import urlopen
# def get(url):
#     return urlopen(url).read()
#
# print(get('http://www.baidu.com'))
#

# 只抓取python的页面
# def f1(url):
#     def f2():
#         print(url)
#     return f2()
#
# f=f1('http://www.python.org')

# 下载python网页的内容
from urllib.request import urlopen
def f1(url):
    def f2():
        print(urlopen(url).read())

    return f2

python=f1('http://www.python.org')

python()

 

posted @ 2018-03-25 21:13  森森2017  阅读(161)  评论(0编辑  收藏  举报