李成笔记网

专注域名、站长SEO知识分享与实战技巧

为什么python高手都爱用闭包?这个实时函数技巧绝了

杂谈

我想很多人都玩过python的闭包,其中最有趣的部分应该就是装饰器了。

但我想很多人应该没运用上闭包的特性——外部局部变量的存储

什么意思呢?其实就是当闭包引用外部的局部变量将会被存储起来,而不会随着函数的结束而释放。

def test_block():
    test_list = []

    def inner():
        test_list.append(1)
        print(test_list)

    return inner


test = test_block()
test()  # [1]
test()  # [1, 1]
test()  # [1, 1, 1]

可以看到,代码中的test_list没有被重新设置为空的列表,而是在闭包中不断添加新的元素。

当被inner()闭包函数捕获以后,test_list就不是局部变量了,它会被存储起来。

今天我将基于这个特性写一个比较有趣的代码,可以随时更新实时函数,只要你传入一个.py文件就可以在任何时候更新函数,就算是程序已经在运行期间也可以。

通过这个方式很容易实现架构风格中的基于规则的架构风格,例如当我们存在一笔消费,正常情况下,满100减5,当发生活动时,需要满100减20,且该规则没有在前期开发中进行实现,那么我们在不进行程序重启的情况下是很难实现规则的改变,因此可以通过本文来实现类似的需求。

一、调用外部函数

我们先来创建一个文件handle.py,里面创建一个函数show()将在后续用到:

# handle.py


def show(name):
    print(f'我叫 {name}')

然后我们来写一个代码,通过文件的路径来调用它的方法:

import importlib

spec = importlib.util.spec_from_file_location(
    name='handle',
    location='handle.py'
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

module.show('小明')

我们用importlib来调用这个文件的show()函数,我们来看看输出情况:

我叫 小明

成功,我们现在只需要.py的文件即可完成调用,完成了最基本的逻辑了。

如果是其他路径模块调用,需要该模块项目中其他的方法或者类,需要将模块的路径加到系统路径下:sys.path.append('xxxxx')

二、逻辑实现

整个代码的逻辑是这样的,通过闭包来记录.py的文件路径,然后每次调用去判断这个文件的修改时间,如果与我们内存中的不一致,说明文件就被改动了,重新加载一次模块即可。

import os
import importlib
import sys
import time


def call(py_file=''):
    # 判断文件是否存在
    if not os.path.exists(py_file):
        raise Exception('Py file does not exist')

    # 文件后缀是否正确
    if not py_file.endswith('py') and not py_file.endswith('pyc'):
        raise Exception('Py file type error')

    # 将模块加入到路径中
    sys.path.append(py_file)

    # 获取咱们的模块名称
    module_name = py_file.split(os.path.sep)[-1]

    if len(module_name.split('.')) > 0:
        module_name = module_name.split('.')[0]

    spec = importlib.util.spec_from_file_location(
        name=module_name,
        location=py_file
    )
    # 模块导入
    module = importlib.util.module_from_spec(spec)
    # 模块可以调用
    spec.loader.exec_module(module)

    # 用来获取module及判断它的文件修改时间
    module_dict = {
        'module': module,
        'update_time': time.strftime('%Y%m%d%H%M%S', time.localtime(os.stat(py_file).st_mtime))
    }

    def inner(*args, **kwargs):
        try:
            update_time_str = time.strftime('%Y%m%d%H%M%S', time.localtime(os.stat(py_file).st_mtime))
        except Exception as e:
            raise e

        # 当前文件修改时间改变,则我们模块需要重新加入
        if module_dict.get('update_time') != update_time_str:
            spec.loader.exec_module(module_dict['module'])
            module_dict["update_time"] = update_time_str
            
        # 外部模块的函数
        return module_dict['module'].show(*args, **kwargs)

    return inner

我们设定了一个局部变量module_dict,用于存储模块的信息,由于它被闭包引用了,因此变成了一个长期变量。

当我们下一次调用时,我们会判断上一次的文件修改时间和本次的区别,如果不同了,则表示模块已经发生改变,我们就做spec.loader.exec_module(module_dict['module'])的操作。

三、测试代码

我们每两秒就跑一次代码,看看执行效果:

f = call(r'xxx\xxx\xxx\handle.py')

while True:
    f('小明')
    time.sleep(2)

然后在中途将调用模块的函数内容改为:

def show(name):
    print(f'我的名字叫 {name}')

测试结果gif:

测试结果

四、结尾

闭包是个比较神奇的概念,很多编程语言都有它的存在,在python中很多人可能只是为了实现装饰器而使用闭包,其实如果开发它的特性能够完成很多有趣的事情。

如果你用闭包玩过什么好玩的,欢迎来留言区留言!

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言