[Python-3000] Default dict iterator should have been iteritems() (original) (raw)
Thomas Wouters thomas at python.org
Thu Sep 6 12:13:33 CEST 2007
- Previous message: [Python-3000] Default dict iterator should have been iteritems()
- Next message: [Python-3000] Default dict iterator should have been iteritems()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 9/6/07, Noam Raphael <noamraph at gmail.com> wrote:
(Sorry, it turns out that I posted this reply only to Nick and not to the list, so I post it again.) On 9/4/07, Nick Coghlan <ncoghlan at gmail.com> wrote: > Containment and iteration really do need to be kept consistent and > having the value matter when checking for dictionary containment would > be outright bizarre. Put the two together and it makes sense for > dictionary iteration and containment tests to both be based on keys. > I absolutely agree that containment and iteration should be kept consistent. I suggest (again, ignoring backwards compatibility completely), that "in" would behave according to the iteration, that is, check if the tuple (key, value) is in dict.items(). If you prefer code: class DreamDict(dict): def iter(self): return self.iteritems() def contains(self, (key, value)): try: myvalue = self[key] except KeyError: return False return value == myvalue Indeed, the suggested "in" operator is not very useful, so you'll usually use haskey. But I actually think that "d.haskey(k)" is clearer than "k in d" - There's no "syntactic" reason why "k in d" should mean "k in d.keys()" and not "k in d.values()".*
None of what you're saying is new. It's all been said back when iteration and containment testing were added to the dict type. The choice was explicitly made for the useful containment test, and the conforming iteration behaviour. The iteration is not actually less useful, it's just different. The net result of 'more useful + just as useful' is 'more useful'. I don't believe the actual experience in the three major releases since it was added, have convinced anyone that it's a bad idea (in fact, I had slight misgivings back then, but none what so ever now.) The mapping types simply don't act as containers of (key, value) pairs.
-- Thomas Wouters <thomas at python.org>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/python-3000/attachments/20070906/109e5dbe/attachment.htm
- Previous message: [Python-3000] Default dict iterator should have been iteritems()
- Next message: [Python-3000] Default dict iterator should have been iteritems()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]