<br><br><div><span class="gmail_quote">On 9/6/07, <b class="gmail_sendername">Noam Raphael</b> <<a href="mailto:firstname.lastname@example.org">email@example.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
(Sorry, it turns out that I posted this reply only to Nick and not to<br>the list, so I post it again.)<br><br>On 9/4/07, Nick Coghlan <<a href="mailto:firstname.lastname@example.org">email@example.com</a>> wrote:<br>> Containment and iteration really do need to be kept consistent and
<br>> having the value matter when checking for dictionary containment would<br>> be outright bizarre. Put the two together and it makes sense for<br>> dictionary iteration and containment tests to both be based on keys.
<br>><br>I absolutely agree that containment and iteration should be kept consistent.<br><br>I suggest (again, ignoring backwards compatibility completely), that<br>"in" would behave according to the iteration, that is, check if the
<br>tuple (key, value) is in dict.items(). If you prefer code:<br><br>class DreamDict(dict):<br> def __iter__(self):<br> return self.iteritems()<br> def __contains__(self, (key, value)):<br> try:<br> myvalue = self[key]
<br> except KeyError:<br> return False<br> return value == myvalue<br><br>Indeed, the suggested "in" operator is not very useful, so you'll<br>usually use has_key. But I actually think that "
d.has_key(k)" is<br>clearer than "k in d" - There's no "syntactic" reason why "k in d"<br>should mean "k in d.keys()" and not "k in d.values()".*</blockquote><div>
<br>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.
<br></div></div><br>-- <br>Thomas Wouters <<a href="mailto:firstname.lastname@example.org">email@example.com</a>><br><br>Hi! I'm a .signature virus! copy me into your .signature file to help me spread!