函数嵌套及闭包函数


目录
  • global与nonlocal关键的使用
  • 函数对象
  • 函数的嵌套调用
  • 函数嵌套的定义
  • 闭包函数
  • 利用闭包函数

global与nonlocal关键的使用

x = 111
def index():
  global x # 局部修改全局变量,需要使用关键字声明
  x = 222
index()
print(x)

name_list = ['jason', 'kevin']
def index():
  name_list.append('junjie')
index()
print(name_list)
# ['jason', 'tony', 'junjie']
'''
如果想在局部修改全局数据:
	如果数据为不可变类型则需要global声明	
	如果数据为可变类型则无需关键字global声明
'''
def index():
  x = 111
  def func():
    # nonlocal x
    x = 222
  func()
  print(x)
index()
# x = 111 , 想要在内部局部修改外部局部, 使用nonlocal,
# 使用 nonlocal 必须是局部对局部修改

函数对象

# 函数名遇到括号就会调用
1. 函数名可以当作变量名赋值
def index():
  print('from index')
a = index
a() 
''' 实际上是在调用index函数'''
2. 函数名还可以当作函数的实参
def index():
  print('from index')
def func(a):
  print(a)
  a() # 函数名遇到括号就会调用
	print('for func)
func(index)
# 3.函数名还可以当作函数返回值
        def index():
    print('from index')
def func():
    print('form func')
    return index 
res = func()
# 若在return尾部添加函数名则可以调用func并接受funce返回值      
res() 					
4.函数名可以当作容器类型(内部可以存放多个数据)的元素
def index():
        print('from index')
l = [111, 222, 333, index()]
print(l)
'''
from index
[111, 222, 333, None]
'''
_________
        
def index():
    print('from index', 'junjie')
    return ['junjie', 'junjun']
l = [111, 222, 333, index()]
print(l)
'''
from index junjie
[111, 222, 333, ['junjie', 'junjun']]
'''

函数的嵌套调用

嵌套调用:函数内部调用其他函数
  def index():
    print('form index')
  def func():
    index()
    print('form func')
  func()
  
def my_max(a, b):
  if a > b:
    return a
  return b

def many_max(x,y,z,m):
    res = my_max(x,y)
    res1 = my_max(res,z)
    res2 = my_max(res1,m)
    return res2
ret = many_max(1,2,3,4)
print(ret)7

函数嵌套的定义

# 函数体内部定义其他函数
# 将复杂的功能全部隐藏起来 暴露一个简单的接口
def all_func(type):
    def register():
        print('注册功能')
    def login():
        print('登录功能')
    def transfer():
        print('转账功能')
    def shopping():
        print('购物功能')
    # 这里仅仅是延时嵌套定义的现象 暂不考虑优化
    if type == '1':
        register()
    elif type == '2':
        login()
    elif type == '3':
        transfer()
    elif type == '4':
        shopping()
    else:
        print('不知道啥功能')

all_func('3')

闭包函数

# 函数在定义阶段名字的查找顺序就已经固定死了
闭:定义在函数内部的函数
包:内部函数使用外部函数名称空间中的名字
# 只有符合上述两个特征的函数才可以称为 "闭包函数"
想要让某一个函数内部永远使用一个参数,并不受外界影响,使用闭包函数

def outer():
    x = 222
    # index 闭包函数
    def index():
        print('from index', x)

    return index
res = outer()
res()

# 函数体代码需要什么,就需要在形参中定义什么,给函数体传参的一种方式


利用闭包函数

# 方式2:利用闭包函数
# def outer(x,y):
#     # x = 2
#     # y = 40
#     def my_max():
#         if x > y:
#             return x
#         return y
#     return my_max
# res = outer(2,40)
# print(res())
# print(res())

import requests
url = 'https://www.baidu.com'
def outer(url):
    def get_content():
        # 爬取百度首页数据
        res = requests.get(url)
        if res.status_code == 200:
            with open(r'xxx.html','wb') as f:
                print('*'.center(30))
                f.write(res.content)
    return get_content

res = outer('https://www.baidu.com')
res()
res()
res()
res1 = outer('https://www.jd.com')
res1()
res1()