dictionary.update() enhancement?

Tim Peters tim.one at home.com
Fri Oct 12 22:54:27 EDT 2001


[Mark J]
> 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.
>
> ...
>
> The above enhancement ...
> would be much faster than anything that can currently be implemented
> within Python (by subclassing dictionary and overriding the update
> method with hand iteration).

Unfortunately, it would be slower than hand iteration.  David Bolen went
into the most detail about that, and he's right:  the overhead of calling
back into a Python function each time would kill performance.

Compare, e.g.,

    x = 3
    for i in xrange(1000000):
        y = x+x

to

    def add(i, j):
        return i+j
    x = 3
    for i in xrange(1000000):
        y = add(x, x)

for a direct comparison of doing a thing by hand inline versus calling a
Python function to do it.

I should also note that the actual implementation of dict.update does no
"collision checking" at all; like its docstring says, it's more akin to

>>> print {}.update.__doc__
D.update(E) -> None.  Update D from E: for k in E.keys(): D[k] = E[k]
>>>

except that if E is a true dict (or in 2.2 also a subclass of dict) it
iterates over E's key+value pairs directly without materializing a list of
the keys (something you can do yourself in 2.2 via the new
dict.iteritems()).

if-speed-is-the-goal-the-status-quo-wins-ly y'rs  - tim





More information about the Python-list mailing list