Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python delattr() Function



ThePython delattr() function is used for deleting an attribute from an object. Once the specified attribute is deleted, it will throw an error if we try to call it using the class object.

Thoseattributes that are specific to an object of the class are termed instance attributes. Two objects of a class may or may not contain common attributes.

Thedelattr() function is one of thebuilt-in functions and does not require any module to import.

Syntax

The syntax of the Pythondelattr() function is shown below −

delattr(object, attribute)

Parameters

The Pythondelattr() function accepts two parameters −

  • object − This parameter represents an object from which the attribute is to be removed.

  • attribute − It indicates the attribute which is to be removed from the specified object.

Return Value

The Pythondelattr() function does not return any values.

delattr() Function Examples

Practice the following examples to understand the use ofdelattr() function in Python:

Example: Basic Use of delattr() Function

The following example shows the basic use of Python delattr() function. Here, we are deleting an attribute of a specified class and with the help of hasattr() function, we are checking whether the attribute is successfully deleted.

class NewClass:   newAttr = "Simply Easy Learning"print("Before deleting the attribute:")    print("Is attribute exist:", hasattr(NewClass, 'newAttr')) delattr(NewClass, 'newAttr')print("After deleting the attribute:")print("Is attribute exist:", hasattr(NewClass, 'newAttr'))

When we run above program, it produces following result −

Before deleting the attribute:Is attribute exist: TrueAfter deleting the attribute:Is attribute exist: False

Example: Delete Attribute of a Class Object Using delattr()

The delattr() function deletes the attribute of a class object. In this example, we are deleting the newAttr attribute that exist inside the constructor body.

class NewClass:   def __init__(self):      self.newAttr = "Simply Easy Learning"newObj = NewClass()print("Before deleting the attribute:") print("Is attribute exist:", hasattr(newObj, 'newAttr'))  delattr(newObj, 'newAttr')print("After deleting the attribute:") print("Is attribute exist:", hasattr(newObj, 'newAttr'))

Following is an output of the above code −

Before deleting the attribute:Is attribute exist: TrueAfter deleting the attribute:Is attribute exist: False

Example: Error During Deleting an Object

If we try to delete an attribute which does not exist using the delattr() function, the code will throw AttributeError as illustrated in the below code.

class NewClass:   passnewObj = NewClass()try:   delattr(newObj, 'newAttr')except AttributeError as exp:   print(exp)

Output of the above code is as follows −

'NewClass' object has no attribute 'newAttr'

Example: Remove Method from a Module Using delattr()

The delattr() function also allows us to remove a method from an existing module of Python. In the code below the math module is imported. Then, with the help of delattr() function, we remove thesqrt() method of the math module.

import mathprint("Before deletion:", hasattr(math, 'sqrt'))  delattr(math, 'sqrt')print("After deletion:", hasattr(math, 'sqrt'))

Following is an output of the above code −

Before deletion: TrueAfter deletion: False
python_built_in_functions.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp