Detecting changes to a dict

geremy condra cto at openmigration.net
Sun Sep 27 11:52:26 EDT 2009


On Sun, Sep 27, 2009 at 5:36 AM, Steven D'Aprano <
steve at remove-this-cybersource.com.au> wrote:

> I'm pretty sure the answer to this is No, but I thought I'd ask just in
> case...
>
> Is there a fast way to see that a dict has been modified? I don't care
> what the modifications are, I just want to know if it has been changed,
> where "changed" means a key has been added, or deleted, or a value has
> been set. (Modifications to mutable values aren't important.) In other
> words, any of these methods count as modifying the dict:
>
> __setitem__
> __delitem__
> clear
> pop
> popitem
> setdefault
> update
>
> Of course I can subclass dict to do this, but if there's an existing way,
> that would be better.
>

d1 = {"a": "b", "c": "d"}
d2 = d1.copy()
assert d1 == d2
d2["e"] = "f"
assert d1 == d2

Is that what you're looking for?

Geremy Condra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090927/07468601/attachment-0001.html>


More information about the Python-list mailing list