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

Nikolas Vanderhoof nikolasrvanderhoof at gmail.com
Sat Apr 7 16:16:46 EDT 2018


This would be a very handy feature, but Coconut (which is just python with
some extra functional-style features) also has support for this kind of
pattern-matching:
http://coconut-lang.org


​Since Coconut will compile to Python (2 or 3) you can just write in
Coconut and use the resulting code in your Python.

Using your first example in coconut would be nearly identical, except I
believe the entire dictionary must be specified (I am not sure about this).

data = {
    'direct': 'some data',
    'nested': {
        'lst_data': [1, 2, 3],
        'int_data': 1
    }
}

{
    'direct': direct,
    'nested': {
        'lst_data': [a, b, c],
        'int_data': _
    }
} = data

print(direct)
print(a)
print(b)
print(c)


And this should print:

On Sat, Apr 7, 2018 at 1:26 PM, thautwarm <yaoxiansamma at gmail.com> 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 } let IsMatchByName record1
> (name: string) = match record1 with | { MyRecord.Name = nameFound;
> MyRecord.ID = _; } when nameFound = name -> true | _ -> false let recordX
> = { Name = "Parker"; ID = 10 } let isMatched1 = IsMatchByName recordX
> "Parker" let isMatched2 = 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/20180407/c4460c2c/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: coconut.png
Type: image/png
Size: 7879 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180407/c4460c2c/attachment-0001.png>


More information about the Python-ideas mailing list