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

ฟังก์ชัน delattr() ใน Python ทำอะไร

Python delattr()

delattr() ลบ Attribute จาก Object หาก Object อนุญาต

Syntax

delattr(object, name)

Parameters: Object, Attribute Name

Returns: None (แค่ลบ Attribute หาก Object อนุญาต)

ตัวอย่าง

class Coordinate:
    x = 12
    y = -7
    z = 0

point1 = Coordinate()
print('x =', point1.x)
print('y =', point1.y)
print('z =', point1.z)

delattr(Coordinate, 'z')
print('--After deleting z attribute--')
print('x =', point1.x)
print('y =', point1.y)
print('z =', point1.z)  # AttributeError

ผลลัพธ์

x = 12
y = -7
z = 0
--After deleting z attribute--
x = 12
y = -7
AttributeError: Coordinate instance has no attribute 'z'