[Python-ideas] Javascript Destructuring Assignment

Ron Adam rrr at ronadam.com
Wed Mar 7 20:37:55 CET 2007


Greg Ewing wrote:
> Ron Adam wrote:
> 
>> I have mixed feeling on it myself.  
> 
> As far as I remember, there weren't really any objective
> reasons given against it, just gut feelings of dislike
> from some people, including Guido. I'm living in hopes
> that he may come round to the idea in time (as he did
> with conditional expressions, for example).
> 
>  > The reason being, (to me), using the '*' for both packing
>  > and unpacking is not the most readable solution.
> 
> To me it doesn't seem any less readable than using
> [...,...] or (...,...) for both packing and unpacking,
> or using * in both function definitions and calls. In
> fact the symmetry is a large part of the beauty of
> the idea.
> 
>> Also the '*' syntax can't be used to unpack nested items.
> 
> I'm not sure what you mean by that.
> 
>     >>> [[a, b, *c], [d, e, *f], *g] = [[1, 2, 3, 4], [5, 6, 7, 8], 9, 10]
> 
>     >>> print a, b, c, d, e, f, g
>     1 2 [3, 4] 5 6 [7, 8] [9, 10]
> 
> Makes perfectly good sense to me.

Didn't say it didn't.

Symmetry is not always the best solution.  Sometimes asymmetry is good 
because it can communicate a different context more clearly. That is more 
of a 'human' issue than machine one.  My opinion is in regards to what 
would be better for me.  It's not a right or wrong point of view and may 
not be better for others.



Hmmm... I think there might be an idea related to this of separating 
formatting from the assignment is such a way that the destination isn't 
specific to the source structure. (food for thought?)

For example: *what if* a sort of string formatting style where used to 
repack objects at their destination?

Where '%%' means repack this object this way at the destination, '[]' is 
unpack, '*' is pack, and ',' are used to indicate place holders.

The example from above could then be...

    >>> data = [[1, 2, 3, 4], [5, 6, 7, 8], 9, 10]
    >>> a, b, c, d, e, f, g = "[[,,*],[,,*],*]" %% data

    >>> print a, b, c, d, e, f, g
    1 2 [3, 4] 5 6 [7, 8] [9, 10]


A chained operation would need to be be done in this next example.  Works 
form left to right.

    >>> data = [[1, 2, 3, 4], [5, 6, 7, 8], 9, 10]
    >>> a, b, c, d = ",,,*" %% "[,[],,]" %% data
    >>> print a, b, c, d
    [1, 2, 3, 4] 5 6 [7, 8, 9, 10]

Possibly the repack specifiers would be pushed onto a stack then pulled 
back off to do the actual repacking at assignment time. (?)


These are just examples to illustrate a concept.  The point here is to 
allow for the separation of the data structure knowledge from assignments 
while still being an operation that can happen at the destination.  That 
may avoid creating intermediate objects.

This type of abstraction may make it easier to interface different types of 
objects and data structures dynamically.

Of course a function could be made to do this, but it would likely be much 
much slower.

     a, b, c = repack(data, repack_specifier)

Ron




More information about the Python-ideas mailing list