簡體   English   中英

為什么 python 允許三引號作為多行注釋和字符串文字

[英]why python allows triple quotes both as multi-line comment as well as string literal

我是 python 的新手,這個問題可能聽起來很傻,但我想澄清一下。 在學習時,我遇到了 python 允許三引號(“””)作為多行注釋和字符串文字的代碼。那么 python 如何知道它是用作注釋還是字符串文字。

"""This
is treated
as comment
and ignored"""

a = """It is
treated as
string literal"""
print(a)

Output:-

It is
treated as
string literal

本質上,如果三引號未分配給變量或文檔字符串,python 將忽略它。 例如

""""This is a module-level docstring""""


def randomFunction():
   """This will be treated as a docstring,
   so if you were to run help(randomFunction) it 
   will display whatever is in here"""


   a = """This is actually assigned to a variable,
   and thus python will interpret it as such"""

   """This by itself is just an unassigned string variable """

字符串也是如此,它們位於兩個引號內。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM