Python 2.2 dictionary question.

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Jul 23 06:46:39 EDT 2001


Duncan Booth <duncan at NOSPAMrcp.co.uk> writes:

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

Please make a bug report on SF, so this won't be forgotten.

> Something like this perhaps?
> 
>    dictionary() -> {}
>    dictionary({'a': 1, 'b': 2}) -> {'a':1, 'b': 2}
>    dictionary(a=1, b=2) -> {'a': 1, 'b': 2}

This would be an option; the other one would be to disallow arguments
to dictionary. Perhaps following UserDict would be appropriate as
well: dictionary would allow an optional dictionary argument. In fact,
UserDict should be implemented as

UserDict = 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)])

Also note that much of this has been discussed before in

http://mail.python.org/pipermail/python-dev/2001-June/015257.html
http://mail.python.org/pipermail/python-dev/2001-June/015258.html
http://mail.python.org/pipermail/python-dev/2001-June/015260.html
http://mail.python.org/pipermail/python-dev/2001-June/015261.html
http://mail.python.org/pipermail/python-dev/2001-June/015265.html
http://mail.python.org/pipermail/python-dev/2001-June/015267.html
http://mail.python.org/pipermail/python-dev/2001-June/015281.html
http://mail.python.org/pipermail/python-dev/2001-June/015288.html
http://mail.python.org/pipermail/python-dev/2001-June/015289.html
http://mail.python.org/pipermail/python-dev/2001-June/015290.html
http://mail.python.org/pipermail/python-dev/2001-June/015292.html

Regards,
Martin



More information about the Python-list mailing list