Andrés Orozco
Andrés Orozco

Reputation: 2661

Documentation String Stub, Python

I'm learning Python because I think is an awesome and powerful language like C++, perl or C# but is really really easy at same time. I'm using JetBrains' Pycharm and when I define a function it ask me to add a "Documentation String Stub" when I click yes it adds something like this:

"""

"""

so the full code of the function is something like this:

def otherFunction(h, w):
    """

    """
    hello = h
    world = w
    full_word = h + ' ' + w

    return full_word

I would like to know what these (""" """) symbols means, Thanks.

Upvotes: 0

Views: 2153

Answers (4)

ewerybody
ewerybody

Reputation: 1590

You can also assign these to a variable! Line breaks included:

>>> multi_line_str = """First line.
... Second line.
... Third line."""
>>> print(multi_line_str)
First line.
Second line.
Third line.

Theoretically a simple string would also work as a docstring. Even multi line if you add \n for linebreaks on your own.:

>>> def somefunc():
...     'Single quote docstring line one.\nAnd line two!''
...     pass
...
>>> help(somefunc)
Help on function somefunc in module __main__:

somefunc()
    Single quote docstring line one.
    And line two!

But triple quotes ... actually triple double quotes are a standard convention! See PEP237 on this also PEP8!

Just for completeness. :)

Upvotes: 0

Lev Levitsky
Lev Levitsky

Reputation: 65791

Triple quotes indicate a multi-line string. You can put any text in there to describe the function. It can even be accessed from the program itself:

def thirdFunction():
    """
    All it does is printing its own docstring.
    Really.
    """
    print(thirdFunction.__doc__)

Upvotes: 4

bkconrad
bkconrad

Reputation: 2650

These are called 'docstrings' and provide inline documentation for Python. The PEP describes them generally, and the wikipedia article provides some examples.

Upvotes: 2

michele b
michele b

Reputation: 1865

""" """ is the escape sequence for strings spanning several lines in python.

When put right after a function or class declaration they provide the documentation for said function/class (they're called docstrings)

Upvotes: 8

Related Questions