Python delattr() Function
Description
delattrThe function is used to delete an attribute.
delattr(x, 'foobar')Equivalent todel x.foobar。
Syntax
delattr syntax:
delattr(object, name)
Parameters
- object -- An object.
- name -- Must be an attribute of the object.
Return Value
None.
Example
The following example demonstrates the usage of delattr:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
class Coordinate:
x = 10
y = -5
z = 0
point1 = Coordinate()
print('x = ',point1.x)
print('y = ',point1.y)
print('z = ',point1.z)
delattr(Coordinate, 'z')
print('--After deleting the z attribute--')
print('x = ',point1.x)
print('y = ',point1.y)
# Trigger an error
print('z = ',point1.z)
Output result:
('x = ', 10)
('y = ', -5)
('z = ', 0)
--删除 z 属性后--
('x = ', 10)
('y = ', -5)
Traceback (most recent call last):
File "test.py", line 22, in <module>
print('z = ',point1.z)
AttributeError: Coordinate instance has no attribute 'z'
Other Extensions
Python Built-in Functions