Suggestion: make sequence and map interfaces more similar

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Mar 31 02:45:30 EDT 2016


On Thursday 31 March 2016 13:45, Paul Rubin wrote:

> Steven D'Aprano <steve at pearwood.info> writes:
>> I want to see an actual application where adding a new key to a
.................^^^^^^^^^^^^^^^^^^^^^

>> mapping is expected to change the other keys.

> directory["mary.roommate"] = "bob"
> directory["mary.address"] = None   # unknown address
> ...
> directory["bob.address"] = "132 Elm Street"
> 
> Since Bob and Mary are roommates, they have the same address, so the
> application might want to update both addresses once it learns one of
> them.

I think I've used that application! It was a nightmare... I had to move 
house seven times before I stopped getting my ex-roomate Stinky Joe's 
electricity bill sent to me.

Nah, only kidding. I've never seen such an application, and I don't believe 
it exists. By your use of the word "might", I'm guessing that you've made it 
up. But even if you didn't, it's still a lousy design. Just because Mary and 
Bob are roommates *now* doesn't mean that they are joined at the hips like 
Siamese twins and are roommates forever. If Mary relocates, the application 
shouldn't automatically relocate Bob as well.

And even if the application does, for some strange reason, this logic should 
be built into the *application itself*, not into dict. By all means subclass 
dict and make your own fancy mapping type that has all sorts of application-
specific smarts (or dumbs, as the case may be). Just don't expect it in the 
standard dict.


-- 
Steve




More information about the Python-list mailing list