Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数定义中带三个引号的字符串文字_Python_String_Literals - Fatal编程技术网

Python 函数定义中带三个引号的字符串文字

Python 函数定义中带三个引号的字符串文字,python,string,literals,Python,String,Literals,我正在学习Python教程,在某个时候,他们讨论了函数的第一条语句如何可以是字符串文字。就本例而言,这个字符串文字似乎是用三个“s完成的,给出了 根据本文档,这将主要用于创建某种自动生成的文档 因此,我想知道这里是否有人可以向我解释这些字符串文字到底是什么?字符串文字只是源代码中按字面意思给出的字符串。它是docstring还是其他字符串并不重要。有关所有详细信息,请参阅,但您现在可能不需要这些详细信息 举几个例子: "abc" 'Guido' r"""Norwegian Blue""" 你所

我正在学习Python教程,在某个时候,他们讨论了函数的第一条语句如何可以是字符串文字。就本例而言,这个字符串文字似乎是用三个
s完成的,给出了

根据本文档,这将主要用于创建某种自动生成的文档


因此,我想知道这里是否有人可以向我解释这些字符串文字到底是什么?

字符串文字只是源代码中按字面意思给出的字符串。它是docstring还是其他字符串并不重要。有关所有详细信息,请参阅,但您现在可能不需要这些详细信息

举几个例子:

"abc"
'Guido'
r"""Norwegian Blue"""
你所说的(我想)叫做(感谢Boud的链接)

现在,如果您在解释器中键入
help(foo)
,您将看到我放在函数中的字符串。您也可以通过
foo来访问该字符串。\uu doc\uuu

当然,字符串文字就是--文字字符串

a = "This is a string literal"  #the string on the right side is a string literal, "a" is a string variable.

它们可以通过多种方式定义:

'single quotes'
"double quotes"
""" triple-double quotes """  #This can contain line breaks!
甚至

#This can contain line breaks too!  See?
''' triple-single 
    quotes '''

它们是字符串,就像任何其他字符串一样,周围有成对的

推荐的形式为三重双引号:

def some_function(s):
    """this is documentation for some_function"""
    print(s)

字符串文字是多个引用选项之一中的字符串,未分配给变量

所以

如果在
def
块之后立即有一个字符串文字,它将成为该方法文档的一部分,称为docstring

以下是您将如何使用它:

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

在Python中,有几种方法可以将字符串分成多行。字符串文本就是其中之一,例如:

s = """Hello,
    world"""
print(s)
>>> Hello,
>>>     world #Notice, that spaces used in the literal are kept.
但是正如您正确注意到的,字符串文本通常用于在线文档

class MyClass(object):
    """This is my class it does this and that.

       It has some cool features and I may tell you about them below.
    """

    def my_method(self):
        """This is a brief description of my method."""

    def important_method(self):
        """Because this method is important, I'm going to tell you
           a lot about it. For example...
        """
在您提问之前,在多行上拆分字符串的一个好方法是神圣的Python括号:

s = ('This is a very very long string. '
     'I have to split it to multiple lines. '
     'Whoa! It works!')
print(s)
>>> This is a very very long string. I have to split it to multiple lines. Whoa! It works!
您可能需要遵循PEP-8,其中规定“每行不得超过80个字符”


快乐的Python黑客!

好吧,看看表达式、文本和字符串的概念会很有帮助

字符串、表达式和文字 在程序中,我们必须表示各种类型的数据。一种类型的数据是整数,另一种类型的数据是浮点数

某种类型的值可以通过各种方式产生,例如,通过各种表达式。表达式是“创建”的程序的任何片段“一个价值。例如,在下面的Python表达式中,表达式
2+2
产生值4。赋值运算符
=
将生成的值4放入名为
i
的变量中:

i = 2+2
给定上述语句,下面的表达式产生相同的值4,但现在此表达式只包含一个变量:

下面,我们通过一个算术表达式生成一个值,然后通过一个变量(也是一个表达式)生成它

然而,语言应该提供直接产生基本值的语法。例如,上面表达式中的
2
检索值2。那些直接产生基本值的表达式称为文字。两个表达式
2+2
4
都产生相同的值4,但第二个表达式是表示操作的一种非常基本的方式,由语言提供,无需执行显式操作,因此它是一个文本

字符串文字和多行字符串 一种非常重要的数据类型是文本,一系列字母、数字和其他字符。这种类型通常称为字符串

这样,字符串文本就是一个生成字符串的文本。在Python中,这些文本通过多种方式进行标记(即,字符串文本有许多语法)。例如,您可以在文字的开头或结尾加上单引号或双引号:

"A string literal"

'Another string literal'
其他方法是将三个单引号或双引号放在相同的位置。在这种情况下,文字可以跨越多行:

"""A single line string literal"""

"""A multiline
string literal"""

'''Another multiline
string literal'''
请注意,无论选择何种语法作为字符串文字,它都不会更改其值。单引号字符串等于具有相同字符的双引号字符串,三引号字符串等于具有相同内容的单引号字符串:

>>> "A single line string literal" == 'A single line string literal'
True

>>> """A single line string literal""" == "A single line string literal"
True

>>> # \n is the character that represents a new line
>>> "A multiline\nstring literal" == """A multiline
string literal""" 
True
docstring以及为什么它们应该是字符串文本 文档所说的是,您可以在方法声明之后放置一个字符串文本,这个文本将被用作文档,我们使用它来调用docstring。使用单引号字符串、双引号字符串、单引号字符串或三引号字符串都无关紧要:它只需要是一个文本

考虑以下功能:

def f1(value):
    "Doc for f1"
    return value + 1

def f2(value):
    """Doc for f2"""
    return value + 2
现在,在python控制台中声明它们,并调用
help(f1)
help(f2)
。请注意,字符串文本的语法并不重要

OTOH,您不能使用其他表达式来生成文档,例如变量或字符串上的操作。因此,下面函数第一行的字符串不是docstring:

它应该是一个文本,因为编译器是显式编写的,以将其作为文档进行管理。但是,编译器不准备将变量、复杂表达式等作为文档进行管理,因此将忽略它们。换句话说,这是设计的

为什么使用三引号字符串作为docstring?

尽管在字符串中可以使用任何形式的字符串文字,但您可以认为文档通常包括非常长的文本,具有多行和段落。因为它包含很多行,所以最好使用接受多行的文本形式,对吗?这就是为什么三引号字符串是编写docstring的首选(但不是强制性)方式的原因

旁注 实际上,您可以在Python函数的任何位置放置字符串文字:

 def flying_literals(param):
    "Oh, see, a string literal!"
    param += 2
    "Oh, see, ANOTHER string literal!"
    return param
    "the above literal is irrelevant, but this one can be still MORE IRRELEVANT"
但是,在中只有文字
i
"A string literal"

'Another string literal'
"""A single line string literal"""

"""A multiline
string literal"""

'''Another multiline
string literal'''
>>> "A single line string literal" == 'A single line string literal'
True

>>> """A single line string literal""" == "A single line string literal"
True

>>> # \n is the character that represents a new line
>>> "A multiline\nstring literal" == """A multiline
string literal""" 
True
def f1(value):
    "Doc for f1"
    return value + 1

def f2(value):
    """Doc for f2"""
    return value + 2
mydoc = "This is doc"
def f3(value):
     mydoc
     return value+3

 def f4(value):
     "This is no documentation " + "because it is concatenated"
     return value+4
 def flying_literals(param):
    "Oh, see, a string literal!"
    param += 2
    "Oh, see, ANOTHER string literal!"
    return param
    "the above literal is irrelevant, but this one can be still MORE IRRELEVANT"