Python - 註釋



Python 註釋

Python 註釋是程式設計師可讀的 Python 原始碼中的解釋或註解。新增它們的目的是為了使原始碼更容易被人理解,Python 直譯器會忽略它們。註釋增強了程式碼的可讀性,並幫助程式設計師更仔細地理解程式碼。

示例

如果我們執行下面給出的程式碼,生成的輸出將簡單地將“Hello, World!”列印到控制檯,因為註釋會被 Python 直譯器忽略,並且不會影響程式的執行:

# This is a comment
print("Hello, World!")  

Python 支援三種類型的註釋,如下所示:

  • 單行註釋
  • 多行註釋
  • 文件字串註釋

Python 中的單行註釋

Python 中的單行註釋以井號 (#) 開頭,並擴充套件到行尾。它們用於提供關於程式碼的簡短解釋或註釋。它們可以放在它們描述的程式碼上方自己的行上,也可以放在程式碼行的末尾(稱為內聯註釋),以提供關於該特定行的上下文或說明。

示例:獨立單行註釋

獨立單行註釋是指佔據整行的註釋,以井號 (#) 開頭。它放在其描述或註釋的程式碼上方。

在此示例中,獨立單行註釋放置在“greet”函式上方:

# Standalone single line comment is placed here
def greet():
   print("Hello, World!")
greet()

示例:內聯單行註釋

內聯單行註釋是指出現在與程式碼相同的行上的註釋,位於程式碼之後,前面有井號 (#)。

此處,單行內聯註釋跟在print("Hello, World!")語句之後 −

print("Hello, World!")  # Inline single line comment is placed here

Python中的多行註釋

在Python中,多行註釋用於提供跨越多行的較長解釋或註釋。雖然Python沒有多行註釋的特定語法,但有兩種常用的方法:連續單行註釋和三引號字串 −

連續單行註釋

連續單行註釋指的是在每一行的開頭使用井號 (#)。此方法通常用於較長的解釋或將程式碼的各個部分隔開。

示例

在這個例子中,多行註釋用於解釋階乘函式的目的和邏輯 −

# This function calculates the factorial of a number
# using an iterative approach. The factorial of a number
# n is the product of all positive integers less than or
# equal to n. For example, factorial(5) is 5*4*3*2*1 = 120.
def factorial(n):
   if n < 0:
      return "Factorial is not defined for negative numbers"
   result = 1
   for i in range(1, n + 1):
      result *= i
   return result

number = 5
print(f"The factorial of {number} is {factorial(number)}")

使用三引號字串的多行註釋

我們可以使用三引號字串(''' 或 """) 來建立多行註釋。這些字串在技術上是字串字面量,但如果它們沒有賦值給任何變數或用於表示式,則可以用作註釋。

此模式通常用於塊註釋或在需要詳細解釋的程式碼部分進行文件說明。

示例

這裡,三引號字串提供了對“gcd”函式的詳細解釋,描述了它的目的和使用的演算法 −

"""
This function calculates the greatest common divisor (GCD)
of two numbers using the Euclidean algorithm. The GCD of
two numbers is the largest number that divides both of them
without leaving a remainder.
"""
def gcd(a, b):
   while b:
      a, b = b, a % b
   return a

result = gcd(48, 18)
print("The GCD of 48 and 18 is:", result)

使用註釋進行文件編寫

在Python中,文件註釋(也稱為文件字串)提供了一種在程式碼中加入文件的方法。這對於解釋模組、類、函式和方法的目的和用法非常有用。有效地使用文件註釋可以幫助其他開發人員理解你的程式碼及其目的,而無需閱讀實現的所有細節。

Python文件字串

在Python中,文件字串是一種特殊的註釋型別,用於記錄模組、類、函式和方法。它們使用三引號(''' 或 """) 編寫,並放在它們所記錄的實體定義的緊後方。

文件字串可以透過程式設計方式訪問,這使得它們成為Python內建文件工具不可或缺的一部分。

函式文件字串示例

def greet(name):
   """
   This function greets the person whose name is passed as a parameter.

   Parameters:
   name (str): The name of the person to greet

   Returns:
   None
   """
   print(f"Hello, {name}!")
greet("Alice")

訪問文件字串

可以使用.__doc__屬性或help()函式訪問文件字串。這使得可以輕鬆地從互動式Python shell或程式碼中直接檢視任何模組、類、函式或方法的文件。

示例:使用.__doc__屬性

def greet(name):
    """
    This function greets the person whose name is passed as a parameter.

    Parameters:
    name (str): The name of the person to greet

    Returns:
    None
    """
print(greet.__doc__)

示例:使用help()函式

def greet(name):
    """
    This function greets the person whose name is passed as a parameter.

    Parameters:
    name (str): The name of the person to greet

    Returns:
    None
    """
help(greet)
廣告