Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colons at beginning of lines in Python comments

What's the function of the colon : before return here? Is that a style thing in Python?

def close(self):
    """ Close this connection.
    :returns: None
    """
    self.conn.close()
    return
like image 612
Jay.Doe Avatar asked Oct 28 '25 04:10

Jay.Doe


1 Answers

Because it is in a docstring (enclosed in triple quotes) it is just like a comment and doesn't do anything other than document your code.

There are various conventions of writing docstrings - this is one of them (the reST format used by Sphinx).

The colons are typically used to describe what parameters the function expects and what the function returns, like so:

"""
This is a reST style.

:param param1: this is a first param
:param param2: this is a second param
:returns: this is a description of what is returned
:raises keyError: raises an exception
"""

In this case it says that the function is expected to return None.

See this post for more details on the various conventions.

like image 164
Dewald Abrie Avatar answered Oct 29 '25 19:10

Dewald Abrie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!