Python delattr() Function
Description
delattr The function is used to delete an attribute.
delattr(x, 'foobar') is equivalent to del x.foobar.
Syntax
delattr Syntax:
delattr(object, name)
Parameters
- object -- the object.
- name -- must be an attribute of the object.
Return Value
none.
Example
The following examples show the use of delattr:
#!/usr/bin/python
-- coding: UTF-8 --
class
Coordinate
:
x
=
10
y
= -
5
z
=
0
point1
=
Coordinate
(
)
(
'
x =
'
,
point1
.
x
)
(
'
y =
'
,
point1
.
y
)
(
'
z =
'
,
point1
.
z
)
delattr
(
Coordinate
,
'
z
'
)
(
'
-- After deleting the z attribute --
'
)
(
'
x =
'
,
point1
.
x
)
(
'
y =
'
,
point1
.
y
)
Trigger an Error
(
'
z =
'
,
point1
.
z
)
Output result:
('x = ', 10)
('y = ', -5)
('z = ', 0)
--after deleting z attribute--
('x = ', 10)
('y = ', -5)
Traceback (most recent call last):
File "test.py", line 22, in
print('z = ',point1.z)
AttributeError: Coordinate instance has no attribute 'z'
Python built-in function