[Python] Why is a docstring sufficient to satisfy a python's definition of a function when its...

Discussão em 'Python' iniciado por Stack, Junho 5, 2025.

  1. Stack

    Stack Membro Participativo

    I discovered this fun feature of Python, where this:

    def do_nothing():
    """Does nothing."""


    is the same as this:

    def do_nothing():
    ...


    I found someone commented this here: "If you give your function a docstring, you don't even need to put any statement after the line with the docstring."

    Could someone explain why the docstring acts as a no-op?

    I have searched through the python language reference, and I see that python's function definition requires the body to have a statement. I looked at the statement definition in CPython, and I don't see any references to an Ellipsis or docstring as a valid statement: https://docs.python.org/3/reference/compound_stmts.html

    Continue reading...

Compartilhe esta Página