पायथन delattr()
यदि ऑब्जेक्ट इसकी अनुमति देता है तो delattr() ऑब्जेक्ट से किसी विशेषता को हटा देता है।
वाक्यविन्यास
delattr() का सिंटैक्स है -
delattr(object, name)
delattr() विधि दो पैरामीटर लेती है -
delattr() कोई मान नहीं लौटाता (कोई नहीं देता)। यह केवल एक विशेषता को हटाता है (यदि वस्तु इसकी अनुमति देती है)।
उदाहरण
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)
# Raises Error
print('z = ',point1.z) आउटपुट
यह आउटपुट देता है
Traceback (most recent call last):
('x = ', 12)
('y = ', -7)
File "C:/Users/~.py", line 28, in <module>
('z = ', 0)
print('z = ',point1.z)
--After deleting z attribute--
('x = ', 12)
AttributeError: Coordinate instance has no attribute 'z'
('y = ', -7)