The perils of bunch/bundle?
I had some discussion on Friday about static vs. dynamic typing (grass is always green, eh?), and our frequent use of the “dictionary with attribute-like notation” object came up (commonly called a “bunch” or “bundle”). A number of people were saying how bad/unsafe it is. Not convinced, I brought the source code up on the projector and asked people: “If I were to set the ‘__’ key, what would happen?” My thinking was that, if this is a data container like a dictionary, what happens when the data conflicts with non-data keys?
A room full of programmers, and no one could answer (actually I still don’t know, nor do I remember if it raised a KeyError and AttributeError in the appropriate circumstances, but I’d guess not). I pointed out the reason this type of object is a problem is because, even though we have several thousand uses of it and it’s been around for a decade, how it works isn’t clear or documented. Static typing doesn’t change that. Bad code is bad code and there’s heaps of bad code in wonderful statically typed languages.
It so happens we have several more implementations of the same object. The only one that has documented edge case behavior is in probably the highest quality area of the codebase. Incidentally, it is only used during testing. So maybe bunches aren’t that useful after all…
I didn’t quite understand the conundrum you were posing.
Regardless, I think bunches should be avoided if the keys are going to be arbitrary. I like to think of them as mini-class-instances. But if you want a container, then use the proper syntax for a container (i.e. the dict syntax) since that conveys better the intent.
I should add that I also dislike named tuples, but for various slightly different different reasons.