我正在阅读 python doc字符串文字,请参阅:
shortstring ::= "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring ::= "'''" longstringitem* "'''" | '"""' longstringitem* '"""'
这些嵌套引号是什么意思?为什么需要它们?
请您参考如下方法:
直接取自词法定义下面的段落:
In plain English: Both types of 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).
在我看来,他们的符号有点困惑,但他们试图向读者展示三重引号(单引号或双引号)在 Python 语法中充当文字。
为清楚起见, 让 ƒ = """ 和 ∆ = ''',然后我们可以在语法中重写该表达式作为
longstring ::= "ƒ" longstringitem* "ƒ" | '∆' longstringitem* '∆'
然后你会像这样写一个 python 文档字符串
def min(x, y):
∆calculates the minimum of two values x, y∆
if x < y:
return x:
return y
或
def min(x, y):
ƒcalculates the minimum of two values x, yƒ
if x < y:
return x:
return y
而不是正常的,
def min(x, y):
"""calculates the minimum of two values x, y"""
if x < y:
return x:
return y
当然这是一个荒谬的例子,但我希望你能明白这一点。
