Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 继承:从父类获取其文档字符串的方法_Python_Docstring - Fatal编程技术网

Python 继承:从父类获取其文档字符串的方法

Python 继承:从父类获取其文档字符串的方法,python,docstring,Python,Docstring,我在玩文档字符串,最终得到了以下代码: import inspect class InheritedDoc(object): """ Decorator to find docstrings from parent classes of the method class. Decorator are interpreted on the import and not on the call, so the inherated docstr

我在玩文档字符串,最终得到了以下代码:

import inspect

class InheritedDoc(object):
    """ Decorator to find docstrings from parent classes of
        the method class.
        Decorator are interpreted on the import and not
        on the call, so the inherated docstrings will be
        read when we call help.

        @type mroOrder: bool
        @param mroOrder: follow the order of the
            method resolution order (True) or reserse it (False).
            Default: True.

        @Note: if this doc is seen in the code, instead of the
            docstring of a superClass, that must means the "()"
            are missing on the call of InheritedDoc:
                @InheritedDoc -> @InheritedDoc()
    """
    __empty_doc = 'No Doc found in parent classes.\n'
    __empty_doc += 'DocString needs to be set.'

    def __init__(self, mroOrder=True):
        """ Constructor
        """
        self.__mro_order = mroOrder

    def __call__(self, method):
        """ If there are decorator arguments, __call__()
            is only called once, as part of the decoration
            process!
            You can only give it a single argument,
            which is the function object.
        """
        # Generated only if help() were used.
        # Listing of super classes of the method class.
        #
        parent_classes = [
            element for name, element \
               in method.func_globals.items() \
                  if inspect.isclass(element)
        ]

        # We don't want to inheritate from the decorator itself.
        #
        parent_classes.remove(self.__class__)

        # Do we follow the order of the MRO or do we reverse it.
        #
        if not self.__mro_order:
            parent_classes = reversed(parent_classes)

        name = method.__name__

        doc = ''

        for parent_class in parent_classes:

            # Testing if the class define the method
            #
            if hasattr(parent_class, name):
                # Getting the method to find in it the doc.
                #
                class_method = eval('parent_class.%s' % name)
                doc = class_method.__doc__

                # We want to define the doc only if the method
                # got a doc set.
                #
                if doc:
                    # The doc is defined and we don't want
                    # to keep on defining it again and again. 
                    #
                    break

        # We may end up here without any docstring if none were 
        # actually set in parent classes.
        #
        method.__doc__ = doc or self.__empty_doc

        return method

无需在开括号内使用行延续。因此,您可以从列表中删除\符号。另外,我不知道python实现之间的可移植性,但在CPython中,mro作为类的
\uuuuuuuuuuuu
属性可用。这种问题实际上不适合stackoverflow。请参阅,具体而言,这是一个讨论请求,而不是一个回答请求。如果您要根据docstring制作一些东西,您应该真正遵循,例如,在单行中使用单行docstring,“docstring是以句点结尾的短语。它规定了函数或方法作为命令的效果”('Constructor'不是很有描述性),etc希望
mro_order
而不是
mrooorder
@ChrisMorgan完全正确。我将改进docstring格式以匹配docstring PEP。