- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python float() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python ascii() Function
- Python pow() Function
- Python type() Function
- Python List Functions
- Python list() Function
- Python insert() Function
- Python append() Function
- Python extend() Function
- Python pop() Function
- Python remove() Function
- Python reverse() Function
- Python sort() Function
- Python sorted() Function
- Python Dictionary Functions
- Python dict() Function
- Python update() Function
- Python get() Function
- Python keys() Function
- Python setdefault() Function
- Python fromkeys() Function
- Python items() Function
- Python popitem() Function
- Python Tuple Function
- Python tuple() Function
- Python Set Functions
- Python set() Function
- Python frozenset() Function
- Python String Functions
- Python split() Function
- Python join() Function
- Python format() Function
- Python replace() Function
- Python Iterator Functions
- Python iter() Function
- Python min() Function
- Python max() Function
- Python sum() Function
- Python count() Function
- Python index() Function
- Python copy() Function
- Python clear() Function
- Python next() Function
- Python filter() Function
- Python enumerate() Function
- Python zip() Function
- Python reversed() Function
- Python Number Functions
- Python abs() Function
- Python bin() Function
- Python oct() Function
- Python hex() Function
- Python round() Function
- Python divmod() Function
- Python complex() Function
- Python File Handling Functions
- Python open() Function
- Python read() Function
- Python readable() Function
- Python readline() Function
- Python readlines() Function
- Python write() Function
- Python writable() Function
- Python writelines() Function
- Python close() Function
- Python seek() Function
- Python tell() Function
- Python flush() Function
- Python fileno() Function
- Python truncate() Function
- Python Class Functions
- Python object() Function
- Python property() Function
- Python getattr() Function
- Python setattr() Function
- Python hasattr() Function
- Python delattr() Function
- Python classmethod() Function
- Python staticmethod() Function
- Python issubclass() Function
- Python super() Function
- Python Misc Functions
- Python all() Function
- Python any() Function
- Python isatty() Function
- Python bool() Function
- Python callable() Function
- Python globals() Function
- Python locals() Function
- Python dir() Function
- Python id() Function
- Python isinstance() Function
- Python map() Function
- Python repr() Function
- Python slice() Function
- Python vars() Function
- Python Advance Functions
- Python help() Function
- Python hash() Function
- Python breakpoint() Function
- Python bytes() Function
- Python bytearray() Function
- Python memoryview() Function
- Python compile() Function
- Python eval() Function
- Python exec() Function
- Python Tutorial
- Python Tutorial
- Python Examples
- Python Examples
Python delattr() Function
The delattr() function in Python is used when we need to delete an attribute from a specified object. For example:
class fresherearth: ID = 1249 Name = "Kenneth" Designation = "Java Content Manager" Country = "Canada" delattr(fresherearth, "Designation")
Using the following statement in above program:
delattr(fresherearth, "Designation")
The attribute named Designation will no longer be available for the class fresherearth, because it gets deleted using the delattr() function.
Python delattr() Function Syntax
The syntax of delattr() function in Python is:
delattr(object, attribute)
The object refers to the object from where the specified attribute has to be delete. The attribute must be enclosed within single or double quote.
Note: Both parameters are required.
Python delattr() Function Example
Here is the modified version of the program that was given at start of this article. With this program, before using the delattr() function to delete a particular attribute from the object, I've printed the value of that attribute:
class fresherearth: ID = 1249 Name = "Kenneth" Designation = "Java Content Manager" Country = "Canada" ob = fresherearth() print("Designation =", ob.Designation) delattr(fresherearth, "Designation")
The output would be:
Designation = Java Content Manager
But if you will try to execute the same statement, that is:
print("Designation =", ob.Designation)
after deleting the attribute Designation. Then here is the error, you'll see:
The exception raised is AttributeError, therefore if you're trying to delete an attribute using the delattr() function, that doesn't exist in specified object. Then in that case too, this exception will get raised. Therefore be sure to handle this exception. Let's create a program to handle this exception while deleting and printing any attribute of the class fresherearth:
class fresherearth: ID = 1249 Name = "Kenneth" Designation = "Java Content Manager" Country = "Canada" ob = fresherearth() try: print("\nDesignation =", ob.Designation) except AttributeError: print("\nThis attribute is not available.") try: delattr(fresherearth, "Designation") except AttributeError: print("\nThe specified attribute does not exist in the specified object.") try: print("\nDesignation =", ob.Designation) except AttributeError: print("\nThis attribute is not available.")
The sample output is shown in the snapshot given below:
« Previous Function Next Function »