python 函数增加元信息

#!/usr/bin/env python3
# _*_ coding:utf-8 _*_
# ========================================================
# Module         :  function_annotations
# Author         :  luting
# Create Date    :  2018/6/4
# Amended by     :  luting
# Amend History  :  2018/6/4
# ========================================================


# 添加函数元信息
def test_01(x: int, y: int) -> int:
    # x:数据类型, -> 返回值数据类型
    return x+y


def test_02(x: int, y: 'default 5'=5) -> int:
    return x+y


# 多参数注释
def test_03(message: dict(type=str, help='the test message')) -> str:
    return message

# 函数的注解存储在函数__annotations__
print(test_03.__annotations__)


# 创建装饰器保留函数元信息
# 使用 functools 库中的 @wraps 装饰器来注解底层包装函数
from functools import wraps


def test_04(func):

    @wraps(func)
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        return result

    return wrapper


@test_04
def login():
    """
    test
    """
    pass

print(login.__doc__)
原文地址:https://www.cnblogs.com/xiaoxiaolulu/p/9132781.html