On Sat, Sep 26, 2020 at 10:43:23PM -0400, Ricky Teachey wrote:
> The problem is that there is lots of existing code like this:
>
> def __setitem__(self, index, value): ...
>
> But the new features will almost certainly lead people to write new code
> like this:
>
> d={}
> obj[**d] = "foo" # no kwd arguments provided here
I don't think that this will almost certainly lead people to write code
like that. Why would you do that deliberately? Apart from wanting to win
a bet or demonstrate a point.
A better argument is that people will write code that unpacks a dict
which is supposed to have keyword arguments, but *accidently* ends up
being empty.
kw = get_keywords() # oops, this returns an empty dict
obj[**kw] = value
which will be equivalent to:
obj[()] = value # or whatever sentinel we choose
I think that counts as *user error*, not a design flaw. Any function
that takes defaults is vulnerable to the same sort of problem:
"I intended to unpack a bunch of arguments (whether keyword or
positional) but they were unexpectedly empty, so I got the default
values. Python is buggy!!!"
No, Python is working as designed.
The only new thing here is that Python is passing a default for the
leftmost parameter, but even that is not precisely *new*. Although the
internal implementation is different, that's how the range API works:
# intended to unpack a two-tuple (start, end)
# but accidentally unpacked a *one*-tuple and got start=0
range(*args)
So honestly, I think there's nothing to see here. Choose a sentinel,
document it, and move on.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/SAOAL7ILYA62AD7L6WHOQLEDFSHISLW2/
Code of Conduct: http://python.org/psf/codeofconduct/