Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

คอมเมนต์ (Comments) ใน Python


เครื่องหมายแฮช # ที่ไม่อยู่ในสตริงลิเทอรัล จะเริ่มคอมเมนต์ ตัวอักษรทั้งหมดหลัง # จนจบบรรทัด เป็นส่วนของคอมเมนต์ และ Python interpreter จะข้ามไป

ตัวอย่าง

#!/usr/bin/python
# First comment
print "Hello, Python!"
# second comment

เอาต์พุต

Hello, Python!

รูปแบบคอมเมนต์ต่างๆ

คอมเมนต์ต่อท้ายคำสั่ง

name = "Madisetti"  # This is again comment

คอมเมนต์หลายบรรทัด (หลายบรรทัด #)

# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

Docstring / Multi-line string เป็นคอมเมนต์

'''
This is a multi-line comment.
Can span multiple lines.
'''

หมายเหตุ: Docstring ("""...""" หรือ '''...''') ไม่ใช่คอมเมนต์จริงๆ แต่เป็นสตริงที่ถูกข้ามไป หากไม่ได้กำหนดให้ตัวแปร มักใช้เป็นเอกสารของฟังก์ชัน/คลาส