Python文档嵌套引号的表示法?

2024-05-17 08:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读pythondoc的字符串文本,看到了:

shortstring     ::=  "'" shortstringitem* "'" | '"' shortstringitem* '"'
longstring      ::=  "'''" longstringitem* "'''" | '"""' longstringitem* '"""'

这些嵌套的引号是什么意思?为什么他们是必要的


Tags: 字符串文本引号longstringshortstringpythondocshortstringitemlongstringitem
1条回答
网友
1楼 · 发布于 2024-05-17 08:22:11

直接摘自以下段落的词汇定义:

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 docstring,如

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

当然,这是一个可笑的例子,但我希望你明白这一点

相关问题 更多 >