Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Thoughts on "extended mapping unpacking"

25 views
Skip to first unread message

Neil Girdhar

unread,
May 24, 2018, 4:38:50 AM5/24/18
to python-ideas
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

Serhiy Storchaka

unread,
May 24, 2018, 8:00:08 AM5/24/18
to python...@python.org
24.05.18 11:38, Neil Girdhar пише:

> 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

p = parameters.pop('some_parameter')
q = parameters.pop('some_other_parameter')
if parameters:
raise ValueError

or

p, q = (lambda some_parameter, some_other_parameter: some_parameter,
some_other_parameter)(**parameters)

_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Neil Girdhar

unread,
May 24, 2018, 11:47:30 AM5/24/18
to python...@googlegroups.com, python...@python.org
On Thu, May 24, 2018 at 8:00 AM Serhiy Storchaka <stor...@gmail.com> wrote:
24.05.18 11:38, Neil Girdhar пише:
> 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

p = parameters.pop('some_parameter')
q = parameters.pop('some_other_parameter')
if parameters:
     raise ValueError

parameters is a Mapping subclass and I don't want to destroy it 
or

p, q = (lambda some_parameter, some_other_parameter: some_parameter,
some_other_parameter)(**parameters)

that works 
_______________________________________________
Python-ideas mailing list
Python...@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

--

---
You received this message because you are subscribed to a topic in the Google Groups "python-ideas" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python-ideas/rupoMkwAhi0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python-ideas...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Serhiy Storchaka

unread,
May 24, 2018, 12:37:41 PM5/24/18
to python...@python.org
24.05.18 18:46, Neil Girdhar пише:

> p = parameters.pop('some_parameter')
> q = parameters.pop('some_other_parameter')
> if parameters:
>      raise ValueError
>
> parameters is a Mapping subclass and I don't want to destroy it

Oh, right. It works if parameters is a var-keyword parameter.

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

George Leslie-Waksman

unread,
May 24, 2018, 1:12:25 PM5/24/18
to Serhiy Storchaka, python...@python.org
I have had plenty of instances where destructuring a mapping would have be convenient. Relating to iterable destructuring, I would expect the syntax to be of the form "variable: key". I also think the curly-braces make it harder to visually parse what's going on. So I might suggest something a little like:

objkey = object()
mydict = {'a': 1, 'b': 2, 'c': 3, 4: 5, None: 6, objkey: 7}
var1: 'a', var2: 4, var3: None, var4: objkey, **rest = mydict
assert var1 == 1
assert var2 == 5
assert var3 == 6
assert var4 == 7
assert rest == {'b': 2, 'c': 3}

MRAB

unread,
May 24, 2018, 1:29:18 PM5/24/18
to python...@python.org
On 2018-05-24 18:08, George Leslie-Waksman wrote:
> I have had plenty of instances where destructuring a mapping would have
> be convenient. Relating to iterable destructuring, I would expect the
> syntax to be of the form "variable: key". I also think the curly-braces
> make it harder to visually parse what's going on. So I might suggest
> something a little like:
>
> objkey = object()
> mydict = {'a': 1, 'b': 2, 'c': 3, 4: 5, None: 6, objkey: 7}
> var1: 'a', var2: 4, var3: None, var4: objkey, **rest = mydict

The problem there is that the key and the value are now the wrong way
round...

Reply all
Reply to author
Forward
0 new messages