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

Steven D'Aprano steve at pearwood.info
Fri Oct 12 06:41:11 EDT 2018


On Thu, Oct 11, 2018 at 01:27:08PM -0400, Chris Barker - NOAA Federal via Python-Dev wrote:
> > On the server side, the application could be doing something like
> > assuming that the kwargs are e.g. column names
> 
> This is exactly a use-case for non-identifier strings in kwargs.

Why not just pass a dict as an argument, rather than (ab)using kwargs?

Instead of:

- building a dict containing non-identifiers;
- unpacking it in the function call;
- have the interpreter re-pack it to a **kwargs;
- and then process it as a dict

we can cut out the two middle steps. So I must admit, I'm perplexed as 
to why people use an extra (superfluous?) ** to unpack a dict that's 
just going to be packed again. I just don't get it. *shrug*

I also wonder whether the use-cases for this would be reduced if we 
introduced verbatim names? 

https://mail.python.org/pipermail/python-ideas/2018-May/050791.html

Keys containing non-identifier characters like spaces and hyphens would 
still need the kwargs trick, but for reserved words you could just 
escape the argument:

def spam(eggs, \while=None):
    ...

spam(eggs=1234, \while=5678)


which frankly looks much better to me than 

spam(eggs=1234, **{"while": 5678})



-- 
Steve


More information about the Python-Dev mailing list