在另一个docstring中包含docstring

2024-06-02 07:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

问题:我想在另一个docstring中使用一个docstring。你知道吗

假设我有以下代码段:

def window(dimensions: tuple):
    '''
    Function to create an app window and return it

    PARAMETERS
    ----------
    dimensions : tuple
        The width and height of the window to create

    RETURNS
    -------
    display.Window
        Class to implement a screen           # Make this equal to Window.__doc__ 
    '''
    class Window:
        '''
        Class to implement a screen
        '''
        def __init__(self, dimensions: tuple):
            pass
    return Window(dimensions)

我想自动设置window的docstring以包含Window的docstring

我听说你可以手动设置docstring,比如:

window.__doc__ = "".join((window.__doc__, Window.__doc__))

但它只在调用函数时执行。你知道吗

另外,我可以使用decorators,但是有没有更简单直观的方法来实现这一点?你知道吗

奖励:有没有办法决定在docstring中的什么位置可以包含另一个?你知道吗

编辑:所以,这个问题似乎有重复的建议,但由于我是在没有修饰师的情况下特别问的,这确实使我的问题有点不同。另外,我在window中使用嵌套类意味着任何更改__doc__:

  1. window内部:在调用函数之前不会发生。
  2. window之外:将不会运行,因为Window是嵌套的。
从目前的情况来看,这两种方法都被排除了。

但答案当然是其中之一。所以答案是重复的,而不是问题。:页

因此,我不得不重新构造我的代码。见下文。你知道吗


Tags: andtodocreturndefcreateimplementwindow
1条回答
网友
1楼 · 发布于 2024-06-02 07:21:52

多亏了@aparpara,我找到了那个答案(当我在网上搜索时,它没有出现),它让我意识到有(可能?)我的具体问题没有解决办法。你知道吗

因此,我必须删除嵌套类才能在函数外部访问它。你知道吗

这是最终版本。你知道吗

# module display_module.py

class Window:
    '''
    Class to implement pygame's screen
    '''
    def __init__(self, dimensions: tuple):
        pass

def window(dimensions: tuple):
    '''
    Function to create an app window and return it

    PARAMETERS
         
    dimensions : tuple
        The width and height of the window to create

    RETURNS
       -
    display.Window
        {0}
    '''
    return Window(dimensions)

window.__doc__ = window.__doc__.format(Window.__doc__.strip())

对这个老问题还有任何答案!你知道吗

相关问题 更多 >