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

Steven D'Aprano steve at pearwood.info
Tue Apr 10 05:21:35 EDT 2018


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


More information about the Python-ideas mailing list