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

การลบสมาชิก Tuple (Delete Tuple Elements) ใน Python

การลบสมาชิก tuple แบบเดี่ยวๆ ไม่เป็นไปได้ มี ทว่ ไม่มีอะไรผิดกับการรวม tuple ใหม่ด้วยสมาชิกที่ไม่ต้องการถูกทิ้ง

เพื่อลบ tuple ทั้งหมดอย่างชัดเจน ให้ใช้คำสั่ง del

ตัวอย่าง

#!/usr/bin/python
tup = ('physics', 'chemistry', 1997, 2000);
print tup;
del tup;
print "After deleting tup : ";
print tup;

ผลลัพธ์ (Output)

ผลลัพธ์ดังนี้ สังเกต exception raised เพราะหลัง del tup tuple ไม่มีอยู่แล้ว:

('physics', 'chemistry', 1997, 2000)
After deleting tup :
Traceback (most recent call last):
  File "test.py", line 9, in 
    print tup;
NameError: name 'tup' is not defined