[Python-ideas] Dictionary destructing and unpacking.

Lucas Wiman lucas.wiman at gmail.com
Thu Jun 8 03:24:48 EDT 2017


>
> It's still true.  In Python, if I need those things in variables
> *frequently*, I write a destructuring function that returns a sequence
> and use sequence unpacking.
>

Yes, you frequently need to write destructuring functions. Of course there
are ways to write them within Python, but they're often sort of ugly to
write, and this syntax would make writing them nicer. I was disagreeing
with your assertion that this is an uncommon use case: it's not, by your
own admission. Of course, it is business logic which can be factored into a
separate method, it's just that it would often be nice to not need to write
such a method or define a NamedTuple class.

If this feature does get added, I think it would make more sense to add it
as part of a general framework of unification and pattern matching. This
was previously discussed in this thread:
https://mail.python.org/pipermail/python-ideas/2015-April/thread.html#32907

- Lucas

On Thu, Jun 8, 2017 at 12:15 AM, Stephen J. Turnbull <
turnbull.stephen.fw at u.tsukuba.ac.jp> wrote:

> Lucas Wiman writes:
>
>  > > Maps with a known, fixed set of keys are relatively uncommon
>  > > in Python, though.
>  >
>  > This is false in interacting with HTTP services, where frequently you're
>  > working with deserialized JSON dictionaries you expect to be in a
> precise
>  > format (and fail if not).
>
> It's still true.  In Python, if I need those things in variables
> *frequently*, I write a destructuring function that returns a sequence
> and use sequence unpacking.  If I don't need the values in a
> particular use case, I use the "a, _, c = second_item_ignorable()"
> idiom.
>
> Or, in a larger or more permanent program, I write a class that is
> initialized with such a dictionary.
>
> If you like this feature, and wish it were in Python, I genuinely wish
> you good luck getting it in.  My point is just that in precisely that
> use case I wouldn't be passing dictionaries that need destructuring
> around.  I believe that to be the case for most Pythonistas.
> (Although several have posted in favor of some way to destructure
> dictionaries, typically those in favor of the status quo don't speak
> up until it looks like there will be a change.)
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170608/0acf3d46/attachment-0001.html>


More information about the Python-ideas mailing list