Thoughts on "extended mapping unpacking"

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

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} On Thu, May 24, 2018 at 9:37 AM Serhiy Storchaka <storchaka@gmail.com> 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 assert var1 == 1 assert var2 == 5 assert var3 == 6 assert var4 == 7 assert rest == {'b': 2, 'c': 3} On Thu, May 24, 2018 at 9:37 AM Serhiy Storchaka <storchaka@gmail.com> wrote:
participants (4)
-
George Leslie-Waksman
-
MRAB
-
Neil Girdhar
-
Serhiy Storchaka