Michael Geary
Michael Geary

Reputation: 28850

What are these quoted strings in a class definition?

I am looking at some Python code that has quoted strings used in a class definition in a way I hadn't seen before:

class Foo:
    """
    Docstring for the class. This one I understand, of
    course. I'm wondering about the two strings below.
    """

    one: str
    "One thing."
    another: str
    "Another thing."

    def __init__(self):
    # etc, etc.

What are those last two quoted strings for? I am guessing that the Python runtime ignores them, and there is some external tooling that uses them?

Upvotes: 1

Views: 148

Answers (1)

user2357112
user2357112

Reputation: 280291

These are probably for Sphinx autodoc. Python ignores them - Python itself doesn't support docstrings for attributes - but Sphinx autodoc recognizes a string literal after an attribute definition as documentation for that attribute. I think annotations probably count for this purpose, but I don't have the setup to test it.

Upvotes: 6

Related Questions