design question: no new attributes
Diez B. Roggisch
deets at nospam.web.de
Thu Mar 1 23:58:57 CET 2007
> One last point. While I remain interested in examples of how
> "late" addition of attributes to class instances is useful,
> I must note that everyone who responded agreed that it
> has been a source of bugs. This seems to argue against a
> general ban on "locking" objects in some way, in some
I've got some examples. I quite often used dynamic attributes in
situations where I wasn't the creator of certain objects, but had to use
them and wanted to link them with my own code.
For example, in various GUI-environments, you've got some sort of tree
view, usually containing item-objects that form the tree.
Now in other languages like C++ or Java, I have to subclass those items
(if possible at all) if I want to associate my own data (for example the
"real" object behind such an item) with them. Or I need to have an
awkward global mapping between tree-items and my data.
But in python, I can just easily add a new .data_object property, and in
whatever event-handler spits a e.g. selected item at me, I'm easily
accessing the data behind it.
Other examples are processing XML-trees that also have associated data
and the like.
More information about the Python-list