[Python-Dev] Arbitrary non-identifier string keys when using **kwargs

Guido van Rossum guido at python.org
Tue Oct 9 20:26:11 EDT 2018


On Tue, Oct 9, 2018 at 5:17 PM Barry Warsaw <barry at python.org> wrote:

> On Oct 9, 2018, at 16:21, Steven D'Aprano <steve at pearwood.info> wrote:
> >
> > On Tue, Oct 09, 2018 at 10:26:50AM -0700, Guido van Rossum wrote:
> >> My feeling is that limiting it to strings is fine, but checking those
> >> strings for resembling identifiers is pointless and wasteful.
> >
> > Sure. The question is, do we have to support uses where people
> > intentionally smuggle non-identifier strings as keys via **kwargs?
>
> I would not be in favor of that.  I think it doesn’t make sense to be able
> to smuggle those in via **kwargs when it’s not supported by Python’s
> grammar/syntax.
>

Well, it currently works in all Python implementations (definitely in
CPython, and presumably in PyPy and Jython because they tend to follow
CPython carefully). The less the spec leaves undefined the better, IMO, and
I fully expect we'll be breaking code that is doing this. So we might as
well make it the law.

For example, in some code bases it's a pretty common pattern to pass dicts
around using **kwds several levels deep, with no intention to unpack it
into individual keyword arguments -- the caller sends a dict, and the
receiver accepts a dict and does dict-y things to it. Sure, they probably
shouldn't be abusing **kwds, but they are, and I can't really blame them --
possibly this code evolved from a situation that did use keyword args.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20181009/b406d4ac/attachment-0001.html>


More information about the Python-Dev mailing list