Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for What is a Python docstring?
Thea
Thea

Posted on

     

What is a Python docstring?

Code documentation is an integral part of Python development experience. Sometimes there may be more documentation in the source than code itself. It helps you understand what a function or class does, what arguments it accepts, and what it returns.

Python provides a handy way to add documentation. Documentation string or docstring is a string literal, and it is used in classes, modules, functions, or method definitions. The docstring is very similar to a comment but is represented with the triple-double quote (""") .

Docstrings are accessible by__doc__ attribute of the objects and also with the built-inhelp() function.

Docstring example:
Alt Text

In this example, we have defined a cat function that returns meow. We have declared a docstring that explains what the function does. To get the docstring of a function, we need to display the doc attribute (print(cat.__doc__))

There are a couple of ways to write docstring, i.e., one-line docstring and multi-line docstring.

One-line Docstrings

Depending on the complexity of the function, method, or class, a one-line docstring may be perfectly appropriate.

For example:
Alt Text

The closing quotes are on the same line as the opening quotes. You can use one of the quotes, i.e., triple single or triple-double quotes. There's no blank line either before or after the docstring.

Multi-line Docstrings

In more complex cases, it is a better idea to give additional information about the code. Multi-line docstrings consist of a summary line like a one-line docstring, followed by a single blank line with the descriptive text.

The general format for writing a multi-line docstring is:
Alt Text

As you can see, docstring improves usability and code readability. It will be easy for a programmer who didn't write the code to understand what this or that function does. The most important thing to remember when working with docstrings is that you write the docstring for people who will use your code.

Yet, docstrings is a very vast topic, and If you would like to learn more, then check out the Python DocStringsPEP257.

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
v6 profile image
🦄N B🛡
// , “It is not so important to be serious as it is to be serious about the important things. The monkey wears an expression of seriousness... but the monkey is serious because he itches."(No/No)
  • Location
    (No/No)
  • Education
    (No/No)
  • Work
    Security
  • Joined

Can you please do a part 2 on doctests?

CollapseExpand
 
highflyer910 profile image
Thea
Frontend Engineer with a passion for Python, Vue, React, and graphic design, In-game Photographer, Introvert, Lifelong learner, Curious cat (='.'=)
  • Email
  • Location
    Batumi, Georgia
  • Education
    Computer and Electrical Engineering
  • Pronouns
    she/her
  • Work
    Frontend Engineer
  • Joined

I am studying about it and eager to share my knowledge about the topic.Thank you for your interest, will write about it soon:)

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Frontend Engineer with a passion for Python, Vue, React, and graphic design, In-game Photographer, Introvert, Lifelong learner, Curious cat (='.'=)
  • Location
    Batumi, Georgia
  • Education
    Computer and Electrical Engineering
  • Pronouns
    she/her
  • Work
    Frontend Engineer
  • Joined

More fromThea

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp