Home >>Python Built-in Functions >Python delattr() Function

Python delattr() Function

Python delattr() Function

Python delattr() function is used to delete the required attribute from the given input object. It takes two parameter that is the name of the object and the name of the attribute to be deleted.

Syntax:
delattr(object, attribute)

Parameter Values

Parameter Description
object This is a required parameter. It defines the given object.
attribute This is a required parameter. It defines the name of the attribute you want to remove.

Example

class Person: name = "Jerry" age = 21 country = "India" state = "Delhi" data = Person() print('Before deleting the attribute--') print('Name =', data.name) print('Age =', data.age) print('Country =', data.country) print('State =', data.state) delattr(Person, 'state') print('After deleting the attribute--') print('Name =', data.name) print('Age =', data.age) print('Country =', data.country) print('State =', data.state)
Output:
Before deleting the attribute--
Name = Jerry
Age = 21
Country = India
State = Delhi

After deleting the attribute--
Name = Jerry
Age = 21
Country = India
Traceback (most recent call last):
File "./mypy.py", line 21, in <module>
AttributeError: 'Person' object has no attribute 'state'

Python Built-in Functions Python abs() Function Python all() Function Python any() Function Python ascii() Function Python bin() Function Python bool() Function Python bytearray() Function Python bytes() Function Python callable() Function Python chr() Function Python compile() Function Python complex() Function Python delattr() Function Python dict() Function Python dir() Function Python divmod() Function Python enumerate() Function Python eval() Function Python exec() Function Python filter() Function Python float() Function Python format() Function Python frozenset() Function Python getattr() Function Python globals() Function Python hasattr() Function Python hash() Function Python help() Function Python hex() Function Python id() Function Python input() Function Python int() Function Python isinstance() Function Python issubclass() Function Python iter() Function Python len() Function Python list() Function Python locals() Function Python map() Function Python max() Function Python memoryview() Function Python min() Function Python next() Function Python object() Function Python oct() Function Python open() Function Python ord() Function Python pow() Function Python print() Function Python property() function Python range() Function Python repr() Function Python reversed() Function Python round() Function Python set() Function Python setattr() Function Python slice() Function Python sorted() Function Python str() Function Python sum() Function Python super() Function Python tuple() Function Python type() Function Python vars() Function Python zip() Function
No Sidebar ads