Python打卡 DAY 27

知识点回顾:

  1. 装饰器的思想:进一步复用

  2. 函数的装饰器写法

  3. 注意内部函数的返回值

作业:

编写一个装饰器 logger,在函数执行前后打印日志信息(如函数名、参数、返回值)

python 复制代码
def logger(func):
    def wrapper(*args, **kwargs):  # args 是元组,kwargs 是字典
        print(f"开始执行函数 {func.__name__},参数: {args}, {kwargs}")
        result = func(*args, **kwargs)
        print(f"函数 {func.__name__} 执行完毕,返回值: {result}")
        return result
    return wrapper

@logger
def multiply(a, b):
    return a * b 

multiply(2, 3)  # 调用 multiply 函数,观察日志输出

@浙大疏锦行

相关推荐
花酒锄作田5 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪8 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽9 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战9 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋15 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama