Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python delattr() Function

Python delattr() Function

Python built-in function Python built-in 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

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

(

)

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)
--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 Python built-in function