简体   繁体   English

“”“不同”,“引号”,“类型”的功能?

[英]“”“Different”“”, “quote”, 'types' in function?

I am trying to figure out if the different quote types make a difference functionally. 我试图弄清楚不同的报价类型在功能上是否有所不同。 I have seen people say its preference for "" or '' but what about """ """ ? 我见过人们说它偏爱""''但是""" """呢? I tested it in a simple code to see if it would work and it does. 我用一个简单的代码对其进行了测试,以查看它是否可以正常工作。 I was wondering if """ triple quotes """ have a functional purpose for defined function arguments or is it just another quote option that can be used interchangeably like "" and '' ? 我想知道""" triple quotes """是否具有定义函数参数的功能,还是只是另一个可以像""''那样互换使用的引号选项?

As I have seen many people post about "" and '' I have not seen a post about """ """ or ''' ''' being used in functions. 如我所见,很多人都发表过有关""''我还没有看到有关在功能中使用""" """''' '''

My question is: Does the triple quote have a unique use as an argument or is it simply interchangeable with "" and '' ? 我的问题是:三重引号是否可以作为参数唯一使用,还是可以与""''互换? The reason I think it might have a unique function is because it is a multi line quote and I was wondering if it would allow a multi line argument to be submitted. 我认为它可能具有独特功能的原因是因为它是多行引号,我想知道它是否允许提交多行参数。 I am not sure if something like that would even be useful but it could be. 我不确定类似的事情是否会有用,但确实有用。

Here is an example that prints out what you would expect using all the quote types I know of. 这是一个示例,它使用我所知道的所有引号类型打印出期望的结果。

def myFun(var1="""different""",var2="quote",var3='types'):
    return var1, var2, var3

print (myFun('All','''for''','one!'))

Result: 结果:

('All', 'for', 'one!')

EDIT: 编辑:

After some more testing of the triple quote I did find some variation in how it works using return vs printing in the function. 在对三重引号进行了更多测试之后,我确实发现在函数中使用return vs print时其工作方式有所不同。

def myFun(var1="""different""",var2="""quote""",var3='types'):
    return (var1, var2, var3)

print(myFun('This',
'''Can
Be
Multi''',
'line!'))

Result: 结果:

('This', 'Can\nBe\nMulti', 'line!')

Or: 要么:

def myFun(var1="""different""",var2="""quote""",var3='types'):
    print (var1, var2, var3)

myFun('This',
'''Can
Be
Multi''',
'line!')

Result: 结果:

This Can
Be
Multi line!

From the docs : 文档

String literals can be enclosed in matching single quotes ( ' ) or double quotes ( " ). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). [...other rules applying identically to all string literal types omitted...] 字符串文字括在一对单引号( ' )或双引号( " ),它们也可以匹配三个单或双引号括组(这些通常被称为三引号字符串)。[...其他适用于所有字符串文字类型的规则也省略了...]

In triple-quoted strings, unescaped newlines and quotes are allowed (and are retained), except that three unescaped quotes in a row terminate the string. 在三引号引起来的字符串中,允许(并保留)未转义的换行符和引号,但一行中三个未转义的引号会终止该字符串。 (A “quote” is the character used to open the string, ie either ' or " .) (“引号”是用于打开字符串的字符,即'" 。)

Thus, triple-quoted string literals can span multiple lines, and can contain literal quotes without use of escape sequences, but are otherwise exactly identical to string literals expressed with other quoting types ( including those using escape sequences such as \\n or \\' to express the same content). 因此,三引号的字符串文字可以跨越多行,并且可以在不使用转义序列的情况下包含文字引号,但在其他方面与用其他引号类型( 包括使用\\n\\'等转义序列的字符串文字)完全相同。表达相同的内容)。

Also see the Python 3 documentation: Bytes and String Literals -- which expresses an effectively identical set of rules with slightly different verbiage. 另请参阅Python 3文档: 字节和字符串文字 -表示一组有效的相同规则,但略有不同。


A more gentle introduction is also available in the language tutorial , which explicitly introduces triple-quotes as a way to permit strings to span multiple lines: 语言教程中也提供了更为温和的介绍,该教程明确引入了三引号,以允许字符串跨越多行:

String literals can span multiple lines. 字符串文字可以跨越多行。 One way is using triple-quotes: """...""" or '''...''' . 一种方法是使用三引号: """..."""'''...''' End of lines are automatically included in the string, but it's possible to prevent this by adding a \\ at the end of the line. 行尾会自动包含在字符串中,但是可以通过在行尾添加\\来防止这种情况。 The following example: 下面的例子:

 print("""\\ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """) 

produces the following output (note that the initial newline is not included): 产生以下输出(请注意,不包括初始换行符):

 Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to 

To be clear, though: These are different syntax , but the string literals they create are indistinguishable from each other. 但要清楚一点:这些是不同的语法 ,但是它们创建字符串文字彼此之间是无法区分的。 That is to say, given the following code: 也就是说,给出以下代码:

s1 = '''foo
'bar'
baz
'''
s2 = 'foo\n\'bar\'\nbaz\n'

there's no possible way to tell s1 and s2 apart from each other by looking at their values: s1 == s2 is true, and so is repr(s1) == repr(s2) . 通过查看它们的值,不可能将s1s2彼此区分开: s1 == s2为true, repr(s1) == repr(s2) The Python interpreter is even allowed to intern them to the same value, so it may (or may not) make id(s1) == id(s2) true depending on details (such as whether the code was run at the REPL or import ed as a module). 甚至允许Python解释器将它们内插为相同的值,因此它可能 (也可能不会)使id(s1) == id(s2) true,具体取决于细节(例如代码是在REPL上运行还是在import运行) ed作为模块)。

FWIW, my understanding is that there's a convention whereby """ """, ''' ''' are used for docstring, which is kinda like a #comment, but is a recallable attribute that can be referenced later. FWIW,我的理解是,有一个约定,其中“””,“”,“'',“''用于文档字符串,有点像#comment,但它是可调用的属性,可以在以后引用。 https://www.python.org/dev/peps/pep-0257/ https://www.python.org/dev/peps/pep-0257/

I'm a beginner too, but my understanding is that using triple quotes for strings is not the best idea, even if there's little functional difference with what you're doing currently (I don't know if there might be later). 我是一个新手太多,但我的理解是,使用三引号字符串是不是最好的主意, 即使有一个与你目前在做什么(我不知道是否有可能是更高版本)的小功能上的差异。 Sticking with conventions is helpful to others reading and using your code, and it seems to be a good rule of thumb that some conventions will bite you if you don't follow them, as in this case where a mal-formatted string with triple quotes will be interpreted as a docstring, and maybe not throw an error, and you'll need to search through a bunch of code to find the issue. 遵守约定对其他阅读和使用代码的人很有帮助,如果您不遵循某些约定,这似乎是一个很好的经验法则,例如在这种情况下,格式错误的字符串带有三引号将被解释为文档字符串,并且可能不会引发错误,并且您将需要搜索一堆代码来查找问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM