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

วิธี comment แต่ละเงื่อนไขใน if statement หลายบรรทัดใน Python

คุณสามารถทำได้โดยตรงหากคุณครอบเงื่อนไข multiline if statements ด้วยวงเล็บ ตัวอย่างเช่น

if (cond1 == 'val1' and
    cond2 == 'val2' and # Some comment
    cond3 == 'val3' and # Some comment
    cond4 == 'val4'):

อย่างไรก็ตาม ทำไม่ได้หากพยายามทำโดยไม่มีวงเล็บ ตัวอย่างเช่น โค้ดต่อไปนี้จะให้ error:

if cond1 == 'val1' and \
    cond2 == 'val2' and \ # Some comment
    cond3 == 'val3' and \ # Some comment
    cond4 == 'val4':