[Python] Templated docstrings

Discussão em 'Python' iniciado por Stack, Novembro 27, 2025.

  1. Stack

    Stack Membro Participativo

    Why doesn't dynamically formatting docstrings work? Is there an acceptable workaround for doing this at function definition time?

    >>> DEFAULT_BAR = "moe's tavern"
    >>> def foo(bar=DEFAULT_BAR):
    ... """
    ... hello this is the docstring
    ...
    ... Args:
    ... bar (str) the bar argument (default: {})
    ... """.format(DEFAULT_BAR)
    ...
    >>> foo.__doc__
    >>> foo.__doc__ is None
    True


    I tried with old-school style %s formatting and that didn't work either.

    Continue reading...

Compartilhe esta Página