Python 装饰者:保持状态

Python 装饰者:保持状态,python,decorator,python-decorators,Python,Decorator,Python Decorators,我需要撰写关于给定信息的信息,如给定函数采用的参数等。我想做的示例是 @author("Joey") @parameter("name", type=str) @parameter("id", type=int) @returns("Employee", desc="Returns employee with given details", type="Employee") def get_employee(name, id): // // Some logic to ret

我需要撰写关于给定信息的信息,如给定函数采用的参数等。我想做的示例是

@author("Joey")
@parameter("name", type=str)
@parameter("id", type=int)
@returns("Employee", desc="Returns employee with given details", type="Employee")
def get_employee(name, id):
     //
     // Some logic to return employee
     // 
decorator的框架可以如下所示:

json = {}
def author(author):
    def wrapper(func):
         def internal(*args, **kwargs):
              json["author"] = name
              func(args, kwargs)
         return internal 
    return wrapepr
def parameter(name, type=None):
    def wrapper(func):
         def internal(*args, **kwargs):
              para = {}
              para["name"] = name
              para["type"] = type
              json["parameters"].append = para
              func(args, kwargs)
         return internal 
    return wrapepr
类似地,参数decorator可以编写如下:

json = {}
def author(author):
    def wrapper(func):
         def internal(*args, **kwargs):
              json["author"] = name
              func(args, kwargs)
         return internal 
    return wrapepr
def parameter(name, type=None):
    def wrapper(func):
         def internal(*args, **kwargs):
              para = {}
              para["name"] = name
              para["type"] = type
              json["parameters"].append = para
              func(args, kwargs)
         return internal 
    return wrapepr
类似地,也可以编写其他处理程序。最后,我可以只调用一个函数,它将获得每个函数的所有格式JSON

最终输出可能是

[
{fun_name, "get_employee", author: "Joey", parameters : [{para_name : Name, type: str}, ... ], returns: {type: Employee, desc: "..."}
{fun_name, "search_employee", author: "Bob", parameters : [{para_name : age, type: int}, ... ], returns: {type: Employee, desc: "..."}
... 
}
]
我不确定如何维护该状态,并知道如何将有关一个函数的数据合并在一起处理


我怎样才能做到这一点呢?

我不知道我是否完全了解您的用例,但将author添加到您当前的函数中是否可行:

func_list = []

def func(var):
     return var

json = {}
json['author'] = 'JohanL'
json['func'] = func.func_name
func.json = json

func_list.append(func.json)

def func2(var):
     return var

json = {}
json['author'] = 'Ganesh'
func2.json = json

func_list.append(func2.json)
这可以使用装饰器实现自动化,如下所示:

def author(author):
    json = {}
    def author_decorator(func):
        json['func'] = func.func_name
        json['author'] = author
        func.json = json
        return func
    return author_decorator

def append(func_list):
    def append_decorator(func):
        func_list.append(func.json)
        return func
    return append_decorator

func_list = []

@append(func_list)
@author('JohanL')
def func(var):
     return var

@append(func_list)
@author('Ganesh')
def func2(var):
     return var
然后您可以访问
json
dict as
func.json
func2.json
或在func_列表中查找函数。请注意,为了使装饰程序正常工作,您必须按照我放置它们的顺序添加它们,并且我没有添加任何错误处理

此外,如果不希望显式传递func_列表,而是使用具有显式名称的全局定义列表,则代码可以简化为:

func_list = []

def author(author):
    json = {}
    def author_decorator(func):
        json['func'] = func.func_name
        json['author'] = author
        func.json = json
        return func
    return author_decorator

def append(func):
    global func_list
    func_list.append(func.json)
    return func

@append
@author('JohanL')
def func(var):
     return var

@append
@author('Ganesh')
def func2(var):
     return var

也许这对您来说已经足够了?

当author
Ganesh出现另一个
func2
时,就会出现问题。我们需要创建输出
[{func:'func',author:'JohanL'},{func:'func2',author:'Ganesh.}]
这种方法对第二个函数有效吗?正如我在更新的示例中所示,这对两个函数都有效。每个都会有一个独特的json dict。感谢@JohanL的解释。对这两个JSON是根据JSON生成的。但最终的输出是所有这些JSON的列表。为此,我们需要创建另一个装饰师,对吗?而这个修饰符只需要在第一个修饰符之后调用?我已经用第二个修饰符更新了我的答案,它将函数附加到func_列表中。谢谢你的回答。这很好,但可以在不显式传递参数
func\u list
的情况下自动执行。我试图将其作为一个库,用户不需要显式地传递参数。