
Hi, has anybody considered adding something like this: a = [1, 2] [ 'x', *a, 'y'] as syntactic sugar for a = [1, 2] [ 'x' ] + a + [ 'y' ]. Notes: - This is a common operation - To me, the splicing form looks much better than the concatenation form - It can be implemented more efficiently than a bunch of list concatenations - This would be similar to the "apply" feature [ foo(*a) ]. The '*' operator is reminiscent of dereferencing operators in C-derived languages. Alternatively it could be '@', like perl's implicit array splicing, and Lisp's ',@' in backquoted lists. - (I know "splicing" traditionally refers to something else in Python, but other languages refer to this as splicing, and the English definition of "splice" applies better to this.) -- Karl 2005-09-18 18:26

"Fredrik Lundh" <fredrik@pythonware.com> wrote:
Karl Chen wrote:
Hi, has anybody considered adding something like this: a = [1, 2] [ 'x', *a, 'y']
as syntactic sugar for a = [1, 2] [ 'x' ] + a + [ 'y' ].
Notes: - This is a common operation
is it?
Not in the code that I read/use. While "not all 3 line functions should be a builtin", "not all <5 character typing savings should be made syntax". - Josiah

On Monday 2005-09-19 06:38, Josiah Carlson wrote:
[ 'x', *a, 'y']
as syntactic sugar for
[ 'x' ] + a + [ 'y' ].
Notes: - This is a common operation
is it?
Not in the code that I read/use. While "not all 3 line functions should be a builtin", "not all <5 character typing savings should be made syntax".
The problems with syntax are 1 It adds cognitive load. 2 It makes your code look like line noise. 3 It reduces options for future development. 4 It complicates the parser. In this instance, I don't think #1 applies; the rule "Putting *foo into a comma-separated list is the same as putting all the elements of foo into that list" isn't actually any harder to remember than the current rule concerning the prefix-* operator. #2 is always debatable. In this instance the proposed new form doesn't look any uglier to me (I speak for no one else) than its predecessor. #3 surely isn't true here; there isn't anything else sensible to do with prefix-* in lists, given the existing use specifically in argument lists. I don't know about #4, but I suspect it (along with the related "it requires work, and there isn't much benefit from it") is the best argument against this proposal. -- g

Gareth McCaughan <gmccaughan@synaptics-uk.com> wrote:
The problems with syntax are
1 It adds cognitive load. 2 It makes your code look like line noise. 3 It reduces options for future development. 4 It complicates the parser.
I don't know about #4, but I suspect it (along with the related "it requires work, and there isn't much benefit from it") is the best argument against this proposal.
I don't think the parser would get measureably more complex, but I believe that any work to support this uncommon (from my experience) operation, rather than the dozens of other usable RFEs in the tracker, would be misspent time. - Josiah

"Greg Ewing" <greg.ewing@canterbury.ac.nz> wrote in message news:432E4BC9.1020100@canterbury.ac.nz...
Karl Chen wrote:
Hi, has anybody considered adding something like this: a = [1, 2] [ 'x', *a, 'y']
as syntactic sugar for a = [1, 2] [ 'x' ] + a + [ 'y' ].
You can write that as a = [1, 2] a[1:1] = a
I'm sure you meant to write: a = [1, 2] b = ['x', 'y'] b[1:1] = a Occasional absence of mind makes other people feel useful! PS actually one *can* write a = [1, 2] ['x', 'y'][1:1] = a since this is not actually an assignment but rather syntactic sugar for a function call, but I don't know how one would use the modified list, since b = ['x','y'][1:1] = a doesn't quite fulfill the initial requirement ;)
participants (6)
-
Christos Georgiou
-
Fredrik Lundh
-
Gareth McCaughan
-
Greg Ewing
-
Josiah Carlson
-
Karl Chen