[Python-ideas] Thoughts on "extended mapping unpacking"

Neil Girdhar mistersheik at gmail.com
Thu May 24 04:38:50 EDT 2018


I was previously constructing an object like this:

tb = TemporalBehavior(**kwargs, **parameters)

where various subclasses were doing things like

def __init__(self, some_kwarg, some_other_kwargs, some_parameter, 
some_other_parameter):

Then I realized that I want to pass the paramters as a dictionary so that I 
can store it.  I changed the code to this:

def __init__(self, some_kwarg, some_other_kwargs, parameters):

but I still need "some_parameter", so I did

some_parmeter = parameters['some_parameter']
some_other_parmeter = parameters['some_other_parameter']

Great, but now I have to check that exactly the list of parameters that I 
need is being sent in, so I need to do something like

if set(parameters) != ('some_parameter', 'some_other_parameter'):
    raise ValueError

It might be nice to do instead

{'some_parameter': p, 'some_other_parameter': q} = parameters

I'm just throwing this suggestion out there.  I realize that this is pretty 
niche, but who knows where Python will be in ten years.

I also know that this is possible (and fairly easy) to implement from when 
I worked on PEP 448.

This is similar to unpacking iterables like this:

a, b = range(2)
a, b, *c = range(5)

It's the mapping version of it:

{'a': a, 'b': b} = some_dict
{'a': a, 'b': b, **c} = some_dict

Best,
Neil
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180524/7a01c7cc/attachment.html>


More information about the Python-ideas mailing list