Message103330
In all versions of CPython right now, the following works.
>>> dict({1:2}, **{3:4})
{1: 2, 3: 4}
Other Python implementations raise TypeError for this; CPython should probably do the same, beginning with deprecating this behaviour in Python 3.2 and removing it in 3.3.
From a python-dev posting[1] by Alex Gaynor:
"""
I ran into the follow behavior while making sure Django works
correctly on PyPy. The following behavior was observed in all tested
versions of CPython (2.5, 3.1):
>>> def f(**kwargs):
... print(kwargs)
...
>>> kwargs = {1: 3}
>>>
>>> dict({}, **kwargs)
{1: 3}
>>> f(**kwargs)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: f() keywords must be strings
>>>
This behavior seems pretty strange to me, indeed PyPy gives the
TypeError for both attempts. I just wanted to confirm that it was in
fact intentional.
"""
Raghuram Devarakonda says (in the same python-dev thread):
"I ran into same issue with Django on Jython yesterday [1] since Jython
too gives TypeError for 'dict({}, **kwargs)'."
Guido, on the suggestion that both the CPython and PyPy behaviour be left as is, and that the behaviour be regarded as implementation defined:
"That is just going to cause some programs to have a portability
surprise. I think one or the other should be fixed. I am fine with declaring dict({}, **{1:3}) illegal, since after all it is abuse of the ** mechanism. We should deprecate it in at least one version though."
[1] http://mail.python.org/pipermail/python-dev/2010-April/099427.html |
|
Date |
User |
Action |
Args |
2010-04-16 15:03:05 | mark.dickinson | set | recipients:
+ mark.dickinson |
2010-04-16 15:03:05 | mark.dickinson | set | messageid: <1271430185.29.0.771672162256.issue8419@psf.upfronthosting.co.za> |
2010-04-16 15:03:03 | mark.dickinson | link | issue8419 messages |
2010-04-16 15:03:01 | mark.dickinson | create | |
|