[python-uk] memoize & ordering of kwargs.items()

Jonathan tartley at tartley.com
Fri Nov 11 12:14:42 CET 2011

On 11/11/2011 09:35, Jonathan wrote:
> On 11/11/2011 09:24, Duncan Booth wrote:
>> pick keys that that hash to the same value modulo the size of the 
>> dictionary. Since the dictionary copy copies the keys in the order 
>> they are stored you will get the same hash conflict in the copy as 
>> the original.
> Brilliant, thank-you. So I was just being 'lucky' in my choices of 
> dict keys. I believe this is the answer I am looking for. René & 
> Ross's point about being mindful of different implementations is still 
> very pertinent in my mind, but I'm happy for now.
> Thanks everyone!
>     Jonathan

For completeness, my final test is therefore:

     def test_not_dependant_on_order_of_kwargs(self):
         calls = []

         def counter(**kwargs):

         for first in ascii_letters:
             for second in ascii_letters:
                 forward = {first:0, second:0}
                 reverse = {second:0, first:0}

                 del calls[:]
                 self.assertEqual(len(calls), 1,
                     '%d for %s,%s' % (len(calls), first, second))

Without the 'sorted', this fails with:
FAIL: test_not_dependant_on_order_of_kwargs (__main__.MemoizeTest)
Traceback (most recent call last):
   File "./test.py", line 58, in test_not_dependant_on_order_of_kwargs
     '%d for %s,%s' % (len(calls), first, second))
AssertionError: 2 for a,i




Jonathan Hartley    tartley at tartley.com    http://tartley.com
Made of meat.       +44 7737 062 225       twitter/skype: tartley

More information about the python-uk mailing list