0

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.

4 Answers 4

8

""" """ 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)

Sign up to request clarification or add additional context in comments.

3 Comments

So, the docstring is like a comment for functions and it says what the functions does? o.O
exactly, see the wikipedia article as bkconrad posted for further info
@AndresOrozco: You'll notice that if you type help(nameOfYourFunction) in the interactive interpreter, it will print out the name of your function and whatever you place in triple quotes directly underneath the definition. It will also do this for all of python's builtin functions, as utilize docstrings as well.
4

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__)

5 Comments

Oh so, it's like the function's description, but is like a comment. So you can't see it when the program is running?
You can, this is what the __doc__ attribute is for. Try to put some text inside the quotes and add the print statement in the executed code. P.S. If you're on Python3, the syntax will be print(otherFunction.__doc__).
well i have somethin like this i.imgur.com/VUfKF.png but i have an error "AtributeError: 'str' object has no attribute 'doc'"
This is because you're actually calling the function there (passing arguments to it), so it gets evaluated and substituted with the returned value. It's now what you need; note how in my example there are no parentheses after the function name. You don't want to call the function, you just refer to its attribute. Also note the double underscore. I'll update the answer a bit.
Oh i understand now... i don't need the parameters cuz' i just want to call the attribute, okay Thank's :)
2

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

1 Comment

Actually, triple-quoted strings are just multiline string literals. Docstrings can be any string literal, it just has to be the first thing in the code block.
0

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. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.