YouTip LogoYouTip

Python3 Func Delattr

# Delete attribute 'name' delattr(obj, 'name') print("After deletion:", obj.__dict__) Output: Before deletion: {'name': 'example', 'value': 42} After deletion: {'value': 42} ## Syntax ```python delattr(object, name) ### Parameters * **object**: The object whose attribute is to be deleted. * **name**: The name of the attribute to delete. ### Return Value The `delattr()` function does not return any value. ## Example ```python class Person: def __init__(self, name, age): self.name = name self.age = age p = Person("Alice", 30) print("Before deletion:", p.__dict__) # Delete attribute 'age' delattr(p, 'age') print("After deletion:", p.__dict__) Output: Before deletion: {'name': 'Alice', 'age': 30} After deletion: {'name': 'Alice'} In this example, we create a class `Person` with two attributes: `name` and `age`. We then create an instance of the class and use `delattr()` to remove the `age` attribute from the object. After deletion, the `age` attribute no longer exists in the object's dictionary. ## Related Functions : Gets the value of an attribute. : Checks if an object has a given attribute. : Sets the value of an attribute. ## Summary The `delattr()` function is used to delete an attribute from an object. It takes two arguments: the object and the name of the attribute to delete. This function is useful when you want to dynamically remove attributes from objects during runtime.
← Python3 If StatementPython3 Func __Import__ β†’