What is the proper way to comment functions in Python?

前端 未结 10 1583
情书的邮戳
情书的邮戳 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...

    0 讨论(0)
  • 2021-01-30 02:42

    You can use three quotes to do it.

    You can use single quotes:

    def myfunction(para1,para2):
      '''
      The stuff inside the function
      '''
    

    Or double quotes:

    def myfunction(para1,para2):
      """
      The stuff inside the function
      """
    
    0 讨论(0)
  • 2021-01-30 02:46

    Use docstrings.

    This is the built-in suggested convention in PyCharm for describing function using docstring comments:

    def test_function(p1, p2, p3):
        """
        test_function does blah blah blah.
    
        :param p1: describe about parameter p1
        :param p2: describe about parameter p2
        :param p3: describe about parameter p3
        :return: describe what it returns
        """ 
        pass
    
    0 讨论(0)
  • 2021-01-30 02:46

    Read about using docstrings in your Python code.

    As per the Python docstring conventions:

    The docstring for a function or method should summarize its behavior and document its arguments, return value(s), side effects, exceptions raised, and restrictions on when it can be called (all if applicable). Optional arguments should be indicated. It should be documented whether keyword arguments are part of the interface.

    There will be no golden rule, but rather provide comments that mean something to the other developers on your team (if you have one) or even to yourself when you come back to it six months down the road.

    0 讨论(0)
提交回复
热议问题