dictionary.update() enhancement?

Mark J maj64 at hotmail.com
Fri Oct 12 11:50:14 EDT 2001


Erik Max Francis <max at alcyone.com> wrote in message news:<3BC64CC4.444A77E7 at alcyone.com>...
> Mark J wrote:
> 
> > A long while ago there was a big debate here regarding the desired
> > behavior of the dictionary update method when it is asked to update a
> > key which already exists -- whether it should keep the existing
> > dictionary value, overwrite the value, or raise an exception, etc.
> > 
> > Looking through the archives, it appears that the following simple,
> > powerful, and flexible alternative was never considered:  have update
> > accept an optional function parameter.  This "collision function"
> > would be called with the two respective values when a key collision
> > occurs.
> 
> At the very least, the collision function would have to have be given
> the key as well, since that could affect the decision.
> 
> But after you're done you end up with a complete system which boils down
> to manually selecting for each and every key, which makes one wonder why
> you just don't it manually in the first place:
> 
> 	d = {...}
> 	other = {...}
> 	for key, value in other:
> 	   if d.has_key(key):
> 	       ... handle collision ...
> 	d.update(other)

Because it's not as straight-forward as saying d.update(other,
collision_handler) and it's about 10 times slower.

Mark J



More information about the Python-list mailing list