Python 2.2 dictionary question.

Duncan Booth duncan at NOSPAMrcp.co.uk
Mon Jul 23 05:20:27 EDT 2001


In Python 2.2a1 the dictionary type object 'dictionary' in builtins doesn't 
currently check its arguments at all, so I can call dictionary(1, [66], 
foo=99) if I really want. I guess that this simply means that the 
implementation isn't complete, but I wondered what the intention was that 
dictionary should return. Something like this perhaps?

   dictionary() -> {}
   dictionary({'a': 1, 'b': 2}) -> {'a':1, 'b': 2}
   dictionary(a=1, b=2) -> {'a': 1, 'b': 2}

The last two at present just return an empty dictionary.

The other construction that might be useful would be to build a list of 
(key, value) pairs into a dictionary, reversing the effect of the items() 
method. This would effectively allow dictionary comprehensions for those 
that want them:
   dictionary([(k, v) for k, v in dict if wanted(k)])

-- 
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