dictionary.update() enhancement?

Erik Max Francis max at alcyone.com
Thu Oct 11 21:52:04 EDT 2001


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)

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ When the solution is simple, God is answering.
\__/ Albert Einstein
    7 sisters productions / http://www.7sisters.com/
 Web design for the future.



More information about the Python-list mailing list