一、函数对象(函数名)

"""函数名遇到括号就会调用"""
# # 用法1:函数名可以当作变量名赋值
# def index():
#     print('from index')
# a = index
# a() # 本质就是在调用index函数

# 用法2:函数名还可以当作函数的实参
def index():
    print('from index')
def func(a):
    print(a)
    a()
    print('from func')
func(index)
"""
<function index at 0x000001E2ECC61E18>
from index
from func
"""

image

# 用法3:函数名还可以当作函数的返回值
def index():
    print('from index')
def func():
    print('from func')
    return index
res = func() # 调用func并接受func的返回值
res()
"""
from func
from index
"""

# 用法4:函数名可以当作容器类型(内部可以存放多个数据)的元素
def index():
    print('from index')
l = [1, 2, 3, index()]
print(l)
"""
from index
[1, 2, 3, None]
"""

二、函数的嵌套调用

# 嵌套调用:函数内部调用其他函数
# def index():
#     print('from index')
# def func():
#     index()
#     print('from 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
#     def index():
#         print('from index', x)
#     return index

# 闭包函数其实是给函数传参的第二种方式
# 方式1:函数体代码需要用到数据 直接在括号内定义形参即可
# def index(username):
#     print(username)
# def my_max(a, b):
#     if a > b:
#         return a
#     return b
# 方式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())

eg:
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()
res1()
res1()

五、装饰器

"""
装饰器并不是一个新的知识
	而是由我们之前所学习的名称空间、函数对象、闭包函数组合而来
"""
器:指的是工具
装饰:给被装饰对象添加额外的功能

装饰器的原则
	开放封闭原则
	开发:对扩展开放
	封闭:对修改封闭

装饰器核心思想
    在不改变被"装饰对象内部代码"和"原有调用方式"的基础之上添加额外功能

def index():
    print('from index')
index()

# 统计index函数的执行时间


import time

# print(time.time())  # 1637036293.0609405
# 获取的结果叫时间戳(运行代码的那一刻距离1970-1-1所经历的秒数)
# time.sleep(3)  # 让程序原地等待3秒
# print('睡饱了')


def index():
    time.sleep(3)
    print('from index')
# 统计index函数的执行时间
# 在函数运行之前统计一次时间
start_time = time.time()
index()
# 在函数运行完毕之后再次统计
end_time = time.time()
# 计算差值
print(end_time - start_time)
posted on 2021-11-16 19:25  幽星  阅读(44)  评论(0)    收藏  举报