代码笔记1

Python的docstring

前提

因为在查阅模型文档时看到了这样的函数,”add_end_docstrings“和”add_start_docstrings_to_model_forward“,因此想搞清楚这两个函数的作用,
同时参考此篇博客 参考

其中两个函数的源码如下:

def add_start_docstrings(*docstr):
    def docstring_decorator(fn):
        fn.__doc__ = "".join(docstr) + (fn.__doc__ if fn.__doc__ is not None else "")
        return fn

    return docstring_decorator

其实代码非常简单,就是在__doc__内加一段文本,返回值是一个内部函数

def add_start_docstrings_to_model_forward(*docstr):
    def docstring_decorator(fn):
        docstring = "".join(docstr) + (fn.__doc__ if fn.__doc__ is not None else "")
        class_name = f"[`{fn.__qualname__.split('.')[0]}`]"
        intro = f"   The {class_name} forward method, overrides the `__call__` special method."
        note = r"""

    

    Although the recipe for forward pass needs to be defined within this function, one should call the [`Module`]
    instance afterwards instead of this since the former takes care of running the pre and post processing steps while
    the latter silently ignores them.

    
"""

        fn.__doc__ = intro + note + docstring
        return fn

    return docstring_decorator

其中:qualname ,查阅文档,这是一个显示从模块的全局作用域到该模块中定义的某个类、函数或方法的“路径”,可以理解为 “pwd” 。所以这里的意思就是取fn这个类最原始的那个类出来。qualified name

你可能感兴趣的:(学习计划,学习)