Fwd: Is there any idea about dictionary destructing?

I'm sorry that I didn't send a copy of the discussions here. ---------- Forwarded message ---------- From: Thautwarm Zhao <yaoxiansamma@gmail.com> Date: 2018-04-09 1:24 GMT+08:00 Subject: Re: [Python-ideas] Is there any idea about dictionary destructing? To: "Eric V. Smith" <eric@trueblade.com> Thank you, Eric. Your links really help me and I've investigated it carefully. After reading them, I found the discussion almost focused on a non-nested data structure. The flatten key-value pairs might be easily replaced by something like x, y, z = [some_dict[k] for k in ('a', 'b', 'c')] I couldn't agree more but, when it comes to nested, some_dict = { 'a': { 'b': { 'c': V1}, 'e': V2 }, 'f': V3 } I agree that there could be other ways as intuitive as the dict destructing to get `V1, V2, V3` instead, however dict destructing refers to the consistency of Python language behaviours. When I'm writing these codes: [a, *b] = [1, 2, 3] The LHS is actually equals to RHS, and if we implement a way to apply this on dictionary {'a': a, 'b': b, '@': c, **other} = {'a': 1, 'b': 2, '@': 3, '*': 4} It also presents that LHS equals to RHS. Dict destructing/constructing is totally compatible to Python unpack/pack, just as what iterable destructing/constructing does. It's neat when we talk about Python's data structures we can talk about the consistency, readability and the expression of intuition. In the real world, the following one could really help when it comes to the field of data storage. some_dict = {'a': [1, 2, 3, {"d": 4, "f": 5}]} {'a': [b, *c, {"d": e, **_}]} = some_dict The LHS doesn't only show the structure of some variable intuitively(this makes review easier, too), but also supplies a way to access data in fewer codes. In the previous talk people have shown multiple usages of dict destructing in real world: - Django Rest Framework validate - Load config files and use them, specifically Yaml/JSON data access. In fact, any other operation on dictionary than simply getting a value from a key might needs dict destructing, just when the task is complicated enough. I do think the usages are general enough now to make us allow similar syntax to do above tasks. P.S: Some other advices in the previous talk like the following: 'a' as x, 'b' as y, 'c' as z = some_dict 'a': x, 'b': y, 'c': z = some_dict mode, height, width = **prefs Either of them conflicts against the current syntax, or does mismatch the consistency of Python language(LHS != RHS). thautwarm 2018-04-08 5:39 GMT+08:00 Eric V. Smith <eric@trueblade.com>:
participants (1)
-
Thautwarm Zhao