Python Class Properties

Python Class Properties (Attributes)

Once an object is created, you have full control over its properties (attributes). You can modify existing properties, add new properties on the fly, or even delete them altogether.


1. Modify Object Properties

You can modify properties on objects like this:

Modifying a Property

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

p1 = Person("John", 36)

// Set the age of p1 to 40 p1.age = 40

print(p1.age) # 40


2. Delete Object Properties

You can delete properties on objects by using the del keyword.

# Delete the age property from the p1 object:
del p1.age

3. Delete Objects

You can also delete the object entirely by using the del keyword:

del p1

Exercise

?

Which keyword is used to delete an object property in Python?