String literal with triple quotes in function definitions

后端 未结 6 1264
陌清茗
陌清茗 2020-12-04 15:41

I am following the Python tutorial and at some point they talk about how the 1st statement of a function can be a String Literal. As far as the example goes, this String Lit

6条回答
  •  情话喂你
    2020-12-04 16:03

    A string literal is a string in one of the many quoting options, that is not assigned to a variable.

    So,

    "String" # string literal
    'string' # string literal
    """
      Multiline
      String
      Literal
    """
    foo = "string variable"
    

    When you have a string literal immediately after a def block, it becomes part of the documentation for that method, and is called a docstring

    def foo(hello):
        """This is part of the documentation for foo"""
    

    This is how you would use it:

    >>> def foo(hello):
    ...     """This is the docstring"""
    ...     pass
    ... 
    >>> foo.__doc__
    'This is the docstring'
    

提交回复
热议问题