Python delattr(object, name) deletes the named argument with the given
name from the
In this tutorial, we will learn about the syntax of Python delattr() function, and learn how to use this function with the help of examples.
The syntax of delattr() function is
|object||Required||A Python object.|
Note: The function removes the attribute if and only if the object allows this operation.
The function returns None.
In this example, we will define a class X with three attributes a, b and c. First we shall print the attributes for X, then delete the attribute c, and print the attributes of X.
We will use dir() builtin function to print the attributes of X.
class X: a = 1 b = 2 c = 3 print('Attributes in X before deleting c attribute') for attr in dir(X): if '__' not in attr: print(attr) delattr(X, 'c') print('Attributes in X after deleting c attribute') for attr in dir(X): if '__' not in attr: print(attr)Try Online
Attributes in X before deleting c attribute a b c Attributes in X after deleting c attribute a b
In this Python Tutorial, we have learnt the syntax of Python delattr() builtin function, and also learned how to use this function, with the help of Python example programs.