美文网首页
【Python】通过装饰器实现 docstring 中使用变量

【Python】通过装饰器实现 docstring 中使用变量

作者: sunfkny | 来源:发表于2022-09-27 13:49 被阅读0次
    def docstring_decorator(s):
        """动态修改docstring装饰器"""
    
        def _decorator(obj):
            obj.__doc__ = str(s)
            return obj
    
        return _decorator
    

    例子

    a = "test"
    @docstring_decorator(f"a={a}")
    def f():
        pass
    print(f.__doc__)  # a=test
    

    相关文章

      网友评论

          本文标题:【Python】通过装饰器实现 docstring 中使用变量

          本文链接:https://www.haomeiwen.com/subject/jzvfartx.html