闭包函数

# _*_ coding: utf-8 _*_
# 闭包函数:
# 闭包的意义:返回的函数对象,不仅仅是一个函数对象,在该函数外还包裹了一层作用域,这使得,该函数无论在何处调用,优先使用自己外层包裹的作用域
# 应用领域:延迟计算(原来我们是传参,现在我们是包起来)

# def outter():
# x=1
# def inner():
# print('from inner',x)
# return inner
#
#
# f=outter()
#
# def foo():
# # print(outter)
# print(f)
# x=111111111111111111111111111111111111 #定义阶段
# f()
# foo()
# 为函数体传值的两种方式
# def foo():
# print('hello %s' %name)
#
# 方式一:直接以参数的形式传入
# def foo(name):
# print('hello %s' %name)
#
# foo('egon')
# foo('egon')
# foo('egon')

# 方式二:闭包函数
# def outter(name):
# # name='egon'
# def foo():
# print('hello %s' %name)
# return foo
#
# f=outter('egon')
# # print(f)
# f()
# f()
# f()
#
# f1=outter('alex')
# f1()
# f1()
# f1()

# pip3 install requests
# import requests

# 问题
# def get():
# response=requests.get(url)
# if response.status_code == 200:
# print(response.text)

# 解决方案一:
# def get(url):
# response=requests.get(url)
# if response.status_code == 200:
# print(response.text)
#
# get('https://www.baidu.com')
# get('https://www.baidu.com')
# get('https://www.baidu.com')

# 解决方案二:

# def outter(url):
# # url='https://www.baidu.com'
# def get():
# response=requests.get(url)
# if response.status_code == 200:
# print(response.text)
# return get
#
# baidu=outter('https://www.baidu.com')
# cnblogs=outter('https://www.cnblogs.com')
原文地址:https://www.cnblogs.com/OutOfControl/p/9710517.html