Sobes.tech
Junior — Middle

Is it possible to dynamically add new properties to an object in Python?

sobes.tech AI

Answer from AI

Yes, in Python, objects can be dynamically extended with new properties after their creation. To do this, simply assign a new value using dot notation. For example:

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

p = Person("Ivan")
p.age = 30  # dynamically adding a new property
print(p.age)  # 30

However, it is important to remember that not all objects support dynamic attribute addition. For example, objects with __slots__ or built-in types may not allow this.