Google News
logo
Python Program to Delete object properties
In Python, you can delete object properties using the `del` keyword followed by the object name and the property you want to delete. Here is an example :
Program :
class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

person = Person("John", 28)

print(person.name)  # Output: John

del person.name

print(person.name)  # Raises an AttributeError: 'Person' object has no attribute 'name'
In the example above, we define a `Person` class with a constructor that initializes the `name` and `age` properties. We then create an instance of the `Person` class called `person` with a name of "John" and an age of 28.

We then print out the value of the `name` property, which is "John". Next, we use the `del` keyword to delete the `name` property from the `person` object.

Finally, we try to print out the value of the `name` property again, but this time we get an `AttributeError` because the `name` property no longer exists.