What is the proper way to comment functions in Python?

前端 未结 10 1592
情书的邮戳
情书的邮戳 2021-01-30 01:58

Is there a generally accepted way to comment functions in Python? Is the following acceptable?

#########################################################
# Create         


        
10条回答
  •  心在旅途
    2021-01-30 02:41

    Use a docstring:

    A string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ special attribute of that object.

    All modules should normally have docstrings, and all functions and classes exported by a module should also have docstrings. Public methods (including the __init__ constructor) should also have docstrings. A package may be documented in the module docstring of the __init__.py file in the package directory.

    String literals occurring elsewhere in Python code may also act as documentation. They are not recognized by the Python bytecode compiler and are not accessible as runtime object attributes (i.e. not assigned to __doc__ ), but two types of extra docstrings may be extracted by software tools:

    1. String literals occurring immediately after a simple assignment at the top level of a module, class, or __init__ method are called "attribute docstrings".
    2. String literals occurring immediately after another docstring are called "additional docstrings".

    Please see PEP 258 , "Docutils Design Specification" [2] , for a detailed description of attribute and additional docstrings...

提交回复
热议问题