[Python-ideas] Is there any idea about dictionary destructing?

Jacco van Dorp j.van.dorp at deonet.nl
Tue Apr 10 05:52:40 EDT 2018


I must say I can't really see the point either. If you say like:

>  {'a': a, 'b': b, **c} = {'a': 1, **{'b': 2}}

Do you basically mean:

c =  {'a': 1, **{'b': 2}}
a = c.pop("a")
b = c.pop("b")  # ?

That's the only thing I could think of.

I think most of these problems could be solved with pop and the
occasional list comprehension like this:

a, b, c = [{'a':1,'b':2,'c':3}.pop(key) for key in ('a', 'b', 'c')]

or for your example:

c =  {'a': 1, **{'b': 2}}  # I suppose this one would generally
                                 # be dynamic, but I need a name here.
a, b = [c.pop(key) for key in ('a', 'b')]

would extract all the keys you need, and has the advantage that
you don't need hardcoded dict structure if you expand it to nested
dicts. It's even less writing, and just as extensible to nested dicts.
And if you dont actually want to destruct (tuples and lists aren't
destroyed either), just use __getitem__ access instead of pop.

2018-04-10 11:21 GMT+02:00 Steven D'Aprano <steve at pearwood.info>:
> On Tue, Apr 10, 2018 at 03:29:08PM +0800, Thautwarm Zhao wrote:
>
>> I'm focused on the consistency of the language itself.
>
> Consistency is good, but it is not the only factor to consider. We must
> guard against *foolish* consistency: adding features just for the sake
> of matching some other, often barely related, feature. Each feature must
> justify itself, and consistency with something else is merely one
> possible attempt at justification.
>
>
>>     {key: value_pattern, **_} = {key: value, **_}
>
> If I saw that, I would have no idea what it could even possibly do.
> Let's pick the simplest concrete example I can think of:
>
>     {'A': 1, **{}} = {'A': 0, **{}}
>
> I cannot interpret what that should do. Is it some sort of
> pattern-matching? An update? What is the result? It is obviously some
> sort of binding operation, an assignment, but an assignment to what?
>
> Sequence binding and unpacking was obvious the first time I saw it. I
> had no problem guessing what:
>
>     a, b, c = 1, 2, 3
>
> meant, and once I had seen that, it wasn't hard to guess what
>
>     a, b, c = *sequence
>
> meant. From there it is easy to predict extended unpacking. But I can't
> say the same for this.
>
> I can almost see the point of:
>
>     a, b, c, = **{'a': 1, 'b': 2, 'c': 3}
>
> but I'm having trouble thinking of a situation where I would actually
> use it. But your syntax above just confuses me.
>
>
>> The reason why it's important is that, when destructing/constructing for
>> built-in data structures are not supported completely,
>> people might ask why "[a, *b] = c" is ok but "{"a": a, **b} = c" not.
>
> People can ask all sorts of questions. I've seen people ask why Python
> doesn't support line numbers and GOTO. We're allowed to answer "Because
> it is a bad idea", or even "Because we don't think it is good enough to
> justify the cost".
>
>
>> If only multiple assignment is supported, why "(a, (b, c)) = d" could be
>> ok? It's exactly destructing!
>
> That syntax is supported. I don't understand your point here.
>
>
>>      >> {'a': a, 'b': b, **c} = {'a': 1, **{'b': 2}}
>>      SyntaxError: can't assign to literal
>>
>> Above example could be confusing in some degree, I think.
>
> I have no idea what you expect it to do. Even something simpler:
>
>     {'a': a} = {'a': 2}
>
> leaves me in the dark.
>
>
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list