dictionary issue (and maybe PEP ... depending on the answer)

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Jun 2 04:42:44 EDT 2003


Aldo Cortesi <aldo at nullcube.com> wrote in
news:mailman.1054538463.26232.python-list at python.org: 

> Thus spake dsavitsk (dsavitsk at ecpsoftware.com):
> 
>> The issue is, this consistently returns the months in order. I don't
>> see any obvious reason that it does, but I can't get it to fail. So,I
>> am wondering if there is a reason, or is it serendipity.
> 
> Pure, blind luck - you are simply seeing an artifact of the
> current Python implementation of dictionaries. You'll find
> that this behaviour disappears for larger dictionary sizes.
> It is also not guaranteed to exist in other versions of
> CPython or in alternative Python implementations. 
> 

While I agree it's not a good idea to depend on this behaviour, it isn't 
actually true that the behaviour disappears for large dictionaries. It will 
however disappear if the dictionary is sparse. If the dictionary isn't 
sparse, but the keys don't start at a number close to 0 then you can also 
get the ordering disrupted, for example keys 250..349 will come out in the 
order 256..349,250..255.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list