[Python-Dev] Very Strange Argument Handling Behavior

Guido van Rossum guido at python.org
Sat Apr 17 17:27:18 CEST 2010


On Sat, Apr 17, 2010 at 5:41 AM, Vinay Sajip <vinay_sajip at yahoo.co.uk> wrote:
> Steve Holden <steve <at> holdenweb.com> writes:
>
>> I'm sure we wouldn't want to go so far as to inhibit this. (Py 3.1)
>>
>> >>> def f(**kwargs):
>> ...   kwargs[1] = "dummy"
>> ...   print(kwargs)
>> ...
>> >>> f(this="Guido", that="Raymond", the_other="Steve")
>> {'this': 'Guido', 1: 'dummy', 'the_other': 'Steve', 'that': 'Raymond'}
>
> I think that according to the proposal, the above snippet would be OK, but
>
> def f(**kwargs):
>    kwargs[1] = 'dummy'
>    g(**kwargs)
>
> would fail at the call of g.

And that is already the status quo. Try it out in your friendly Python
interpreter.

The *only* thing that should be *changed* is for the dict() builtin to
insist that if it receives a dict containing keyword arguments the
keys must all be strings. This *only* affects the dict() builtin. It
has keyword arguments so that instead of {'foo': 1, 'bar': 2} you can
write dict(foo=1, bar=2), which arguably is more readable because it
doesn't have so many quotes. OTOH calling dict(x, **y) is a weird
hack. Note that if you wrote a wrapper function in Python around
dict() with the same behavior there would be no way to prevent the
check that all the keys are strings.

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-Dev mailing list