Prev: Any Swisses here?
Next: Can't Add Variable
From: Steven D'Aprano on 4 Jan 2010 21:35 On Tue, 05 Jan 2010 00:52:56 +0000, r0g wrote: > I'd be strongly inclined to think the result would be the sequence on > the left with the data from the second sequence appended to it. What's > wrong with a little duck typing here eh? That's not the existing behaviour. List concatenation doesn't mutate the left hand list, it creates a new list: >>> L = [1, 2, 3] >>> L2 = L + [4, 5, 6] >>> L [1, 2, 3] >>> L2 [1, 2, 3, 4, 5, 6] But if you insist on in-place modification, why do you prefer appending the right hand sequence to the left instead of prepending the left hand sequence to the right? -- Steven
From: r0g on 4 Jan 2010 22:01
Steven D'Aprano wrote: > On Tue, 05 Jan 2010 00:52:56 +0000, r0g wrote: > >> I'd be strongly inclined to think the result would be the sequence on >> the left with the data from the second sequence appended to it. What's >> wrong with a little duck typing here eh? OK, I hadn't read all the other responses when I posted, some of which make fair points why this wouldn't be wise. Fair enough. > > That's not the existing behaviour. List concatenation doesn't mutate the > left hand list, it creates a new list: > I would expect it to append. That's my prejudice though, as I do that far more often :/ > >>>> L = [1, 2, 3] >>>> L2 = L + [4, 5, 6] >>>> L > [1, 2, 3] >>>> L2 > [1, 2, 3, 4, 5, 6] > > > But if you insist on in-place modification, why do you prefer appending > the right hand sequence to the left instead of prepending the left hand > sequence to the right? > > > In-place seems more natural for a mutable type. I admit the left right thing is my prejudice though, western cultural bias I suppose. Its not entirely unprecedented though, the parser reads left to right and the leftmost terms take precedent in lazy logic evaluation. Still, the responses to this have convinced me the + operator shouldn't make assumptions, I'm more open to how += works though as it implies in-place and the left over right precedent quite nicely. Roger. |