Python delattr() built-in function

From the Python 3 documentation

This is a relative of setattr(). The arguments are an object and a string. The string must be the name of one of the object’s attributes. The function deletes the named attribute, provided the object allows it. For example, delattr(x, 'foobar') is equivalent to del x.foobar.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

>>> person = Person("John", 30)
>>> delattr(person, 'age')
>>> person.__dict__
# {'name': 'John'}

class Car:
    def __init__(self, make, model):
        self.make = make
        self.model = model

>>> car = Car("Toyota", "Corolla")
>>> try:
...     delattr(car, 'year')
... except AttributeError as e:
...     print(f"Error: {e}")
# Error: 'Car' object has no attribute 'year'

Ups! Nothing here yet!

This is a great opportunity for you to collaborate! Hit the link at the end of this page and add some examples and a brief description. If you don't know where to start, the Python 3 documentation will lead you in the right direction.

Subscribe to pythoncheatsheet.org

Join 10.900+ Python developers in a two times a month and bullshit free publication , full of interesting, relevant links.