2

For further clarification, C# has the '///' directive which invokes the super-secret-styled Comments which allow you to have nice comments built into intellisense. Java has the '@' directive that allows you to have nice comments as well.

Does Python have something like this?

4 Answers 4

8

They are called docstrings in Python. See the documentation.

A nice feature are the code samples (explained here). They allow to put code in the documentation:

>>> 1 + 1
2
>>>

While this doesn't look like much, there is a tool which can scan the docstrings for such patterns and execute this code as unit tests. This makes sure that Python documenation doesn't go out of date (unlike in other languages).

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

1 Comment

For those who are interested, the module to use the docstring for unittests is called doctest.
2

The convention is well documented in PEP 257.

To summarize, add triple-quoted strings as the first statement in any class or function.

There's also some history that's worth a read if you have time in PEP 256.

Comments

0

In python a docstring can be viewed via commands.

class myClass:
    """
      This is some documentation for the class.
      method1()
      method2()
    """
    def method1(p1):
        ....
        ...
        ...
    def method2():
        ...
        ...

v = myClass

you can then view the docstring by using either

v.__doc__

or

help(myClass)

1 Comment

it's not a comment, it's a doc string. Comments in Python start with a # sign.
0

In Python the resource you are looking for is called doc string. Other people have suggested to read the documentation, and I suggest to check this link: Sphinx Project. This is a library that helps you build and documentation, which can be built from doc string.

Comments

Your Answer

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