Why use setattr() in Python?


#1

Can someone please explain why we would use setattr(x, 'foobar', 123) instead of x.foobar = 123
What is the purpose of the setattr() function and how does it add value?

Thanks

`


#2

We cannot write x.y = z if y is not a defined attribute.

We can write, setattr(x, y, z) at any time, though. y is now dynamic.