How to Use delattr() Function in PythonPython Basics


The delattr() function deletes the named attribute from the object if allowed.

Example

#!/usr/bin/python
# -*- coding: UTF-8 -*-

class temp:
    a = 100
    s = -5
    d = 0

point1 = temp()

print('a = ', point1.a)
print('s = ', point1.s)
print('d = ', point1.d)

delattr(temp, 'd')

print('after delete the z')
print('a = ', point1.a)
print('s = ', point1.s)

# Trigger error
print('z = ', point1.d)

Output:

a =  100
s =  -5
d =  0
after delete the z
AttributeError: 'temp' object has no attribute 'd'
a =  100
s =  -5

Syntax

delattr(object, name)

Parameters

Name Description
object The object from which name attribute is to be removed.
name A string must be the name of the attribute to be removed from the object.

Return Value

It doesn’t return any value.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us