I do Java programming and recently started learning Python via the official documentation.
I see that we can dynamically add data attributes to an instance object unlike in Java:
class House:
pass
my_house = House()
my_house.number = 40
my_house.rooms = 8
my_house.garden = 1
My question is, in what situations is this feature used? What are the advantages and disadvantages compared to the way it is done in Java?
It can also be used when dynamically creating classes; see for instance this tutorial:
http://onlamp.com/pub/a/python/2003/04/17/metaclasses.html?page=1
or this one on Mix-ins, a programming technique that uses this capability to provide better encapsulation and modularity to object oriented code:
http://www.linuxjournal.com/article/4540
The first tutorial uses
setattr(classname, "propertyname", value)instead of theclassname.property = valuesyntax, but they are the same.