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

Thautwarm Zhao yaoxiansamma at gmail.com
Mon Apr 9 07:54:34 EDT 2018


I'm sorry that I didn't send a copy of the discussions here.

---------- Forwarded message ----------
From: Thautwarm Zhao <yaoxiansamma at 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 at 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 at trueblade.com>:

> There was a long thread last year on a subject, titled "Dictionary
> destructing and unpacking.":
> https://mail.python.org/pipermail/python-ideas/2017-June/045963.html
>
> You might want to read through it and see what ideas and problems were
> raised then.
>
> In that discussion, there's also a link to an older pattern matching
> thread:
> https://mail.python.org/pipermail/python-ideas/2015-April/032907.html
>
> Eric
>
> On 4/7/2018 1:26 PM, thautwarm wrote:
>
>> We know that Python support the destructing of iterable objects.
>>
>> m_iter= (_for _in range(10))
>> a,*b, c= m_iter
>>
>> That's pretty cool! It's really convenient when there're many corner
>> cases to handle with iterable collections.
>> However destructing in Python could be more convenient if we support
>> dictionary destructing.
>>
>> In my opinion, dictionary destructing is not difficult to implement and
>> makes the syntax more expressive. A typical example is data access on
>> nested data structures(just like JSON), destructing a dictionary makes the
>> logic quite clear:
>>
>> data= {
>>      "direct": "some data",
>>      "nested": {
>>          "lst_data": [1,2,3],
>>          "int_data": 1
>> }
>> }
>> {
>>     "direct": direct,
>>      "nested": {
>>          "lst_data": [a, b, c],
>>      }
>> }= data
>>
>>
>> Dictionary destructing might not be very well-known but it really helps.
>> The operations on nested key-value collections are very frequent, and the
>> codes for business logic are not readable enough until now. Moreover Python
>> is now popular in data processing which must be enhanced by the entire
>> support of data destructing.
>>
>> Here are some implementations of other languages:
>> Elixir, which is also a popular dynamic language nowadays.
>>
>> |iex> %{} = %{:a => 1, 2 => :b} %{2 => :b, :a => 1} iex> %{:a => a} =
>> %{:a => 1, 2 => :b} %{2 => :b, :a => 1} iex> a 1 iex> %{:c => c} = %{:a =>
>> 1, 2 => :b} ** (MatchError) no match of right hand side value: %{2 => :b,
>> :a => 1}|
>>
>> And in F#, there is something similar to dictionary destructing(actually,
>> this destructs `struct` instead)
>> type MyRecord = { Name: string; ID: int } letIsMatchByName record1 (name:
>> string) = matchrecord1 with| { MyRecord.Name = nameFound; MyRecord.ID = _;
>> } whennameFound = name -> true| _ -> falseletrecordX = { Name = "Parker";
>> ID = 10} letisMatched1 = IsMatchByName recordX "Parker"letisMatched2 =
>> IsMatchByName recordX "Hartono"
>>
>> All of them partially destructs(or matches) a dictionary.
>>
>> thautwarm
>>
>>
>>
>> _______________________________________________
>> 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/
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180409/89f22f51/attachment.html>


More information about the Python-ideas mailing list