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

การทำงานของ Del Operator บน Tuple ใน Python

ไม่สามารถลบ tuple elements แยกต่างหากได้ สร้าง tuple ใหม่โดยไม่รวม elements ที่ไม่ต้องการด้วย list comprehensions หรือ slicing ได้ เพื่อลบ tuple ทั้งอ็อบเจกต์ ใช้ del statement

ตัวอย่าง

tup = ('physics', 'chemistry', 1997, 2000)
print(tup)
del(tup)
print("After deleting tup : ")
print(tup)

จะได้ exception เพราะหลัง 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