Re: Multi-dimensional list initialization

2012-11-09 Thread Ethan Furman
Prasad, Ramit wrote: Dennis Lee Bieber wrote: Of course, if one has a language that, for some reason, evaluates right-to-left (APL, anyone), then x := x - x - x becomes x := x - 0 Is that not the same as x:=-x? No, its the same as 'x = x'. ~Ethan~ -- http://mail.p

RE: Multi-dimensional list initialization

2012-11-09 Thread Prasad, Ramit
Dennis Lee Bieber wrote: > > On Fri, 9 Nov 2012 17:07:09 +1100, Chris Angelico > declaimed the following in gmane.comp.python.general: > > > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence > > wrote: > > > On 07/11/2012 01:55, Steven D'Aprano wrote: > > >> > > >> > > >> Who knows? Who cares? No

Re: Multi-dimensional list initialization

2012-11-09 Thread Chris Angelico
On Sat, Nov 10, 2012 at 2:05 AM, rusi wrote: > In x86 assembler > mov ax, 0 > is 4 bytes Three bytes actually, B8 00 00 if my memory hasn't failed me. BA for DX, B9 ought to be BX and BB CX, I think. But yes, the xor or sub is two bytes and one clock. ChrisA -- http://mail.python.org/mailman/li

Re: Multi-dimensional list initialization

2012-11-09 Thread rusi
On Nov 9, 11:37 am, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: > > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence > > wrote: > >> On 07/11/2012 01:55, Steven D'Aprano wrote: > > >>> Who knows? Who cares? Nobody does: > > >>> n -= n > > >> But I've seen this

Re: Multi-dimensional list initialization

2012-11-08 Thread Mark Lawrence
On 09/11/2012 06:37, Steven D'Aprano wrote: On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence wrote: On 07/11/2012 01:55, Steven D'Aprano wrote: Who knows? Who cares? Nobody does: n -= n But I've seen this scattered through code:

Re: Multi-dimensional list initialization

2012-11-08 Thread Chris Angelico
On Fri, Nov 9, 2012 at 5:37 PM, Steven D'Aprano wrote: > On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: >> Can you enlighten us as to how this is better than either: >> x := -x >> or >> x := 0 - x >> ? I'm not seeing it. > > I'm hoping that Mark intended it as an example of crappy cod

Re: Multi-dimensional list initialization

2012-11-08 Thread Steven D'Aprano
On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: > On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence > wrote: >> On 07/11/2012 01:55, Steven D'Aprano wrote: >>> >>> >>> Who knows? Who cares? Nobody does: >>> >>> n -= n >>> >>> >> But I've seen this scattered through code: >> >> x := x - x -

Re: Multi-dimensional list initialization

2012-11-08 Thread Chris Angelico
On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence wrote: > On 07/11/2012 01:55, Steven D'Aprano wrote: >> >> >> Who knows? Who cares? Nobody does: >> >> n -= n >> > > But I've seen this scattered through code: > > x := x - x - x Can you enlighten us as to how this is better than either: x := -x or

Re: Multi-dimensional list initialization

2012-11-08 Thread Mark Lawrence
On 07/11/2012 01:55, Steven D'Aprano wrote: Who knows? Who cares? Nobody does: n -= n But I've seen this scattered through code: x := x - x - x -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list

Re: Multi-dimensional list initialization

2012-11-08 Thread 88888 Dihedral
On Monday, November 5, 2012 3:07:12 PM UTC+8, Chris Rebert wrote: > On Sun, Nov 4, 2012 at 10:27 PM, Demian Brecht wrote: > > > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D > > matrix" (running 2.7.3, non-core libs not allowed): > > > > > > m = [[None] * 4] * 4 This

Re: Multi-dimensional list initialization

2012-11-08 Thread Ian Kelly
On Thu, Nov 8, 2012 at 1:26 AM, Andrew Robinson wrote: > OK: Then copy by reference using map: > > values = zip( map( lambda:times, xrange(num_groups) ) ) > if len(values) < len(times) * num_groups ... > > Done. It's clearer than a list comprehension and you still really don't > need a li

Re: Multi-dimensional list initialization

2012-11-08 Thread wrw
On Nov 7, 2012, at 11:51 PM, Andrew Robinson wrote: > On 11/07/2012 04:00 PM, Steven D'Aprano wrote: >> Andrew, it appears that your posts are being eaten or rejected by my >> ISP's news server, because they aren't showing up for me. Possibly a side- >> effect of your dates being in the distant p

Re: Multi-dimensional list initialization

2012-11-08 Thread Andrew Robinson
On 11/07/2012 11:09 PM, Ian Kelly wrote: On Wed, Nov 7, 2012 at 8:13 PM, Andrew Robinson wrote: OK, and is this a main use case? (I'm not saying it isn't I'm asking.) I have no idea what is a "main" use case. Well, then we can't evaluate if it's worth keeping a list multiplier around at all

Re: Multi-dimensional list initialization

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 8:13 PM, Andrew Robinson wrote: > OK, and is this a main use case? (I'm not saying it isn't I'm asking.) I have no idea what is a "main" use case. > There is a special keyword which signals the new type of comprehension; A > normal comprehension would say eg: '[ foo for

Re: Multi-dimensional list initialization

2012-11-07 Thread Andrew Robinson
On 11/07/2012 04:00 PM, Steven D'Aprano wrote: Andrew, it appears that your posts are being eaten or rejected by my ISP's news server, because they aren't showing up for me. Possibly a side- effect of your dates being in the distant past? Date has been corrected since two days ago. It will remai

Re: Multi-dimensional list initialization

2012-11-07 Thread Steven D'Aprano
On Wed, 07 Nov 2012 16:24:22 -0800, Andrew Robinson wrote: > On 11/07/2012 01:01 PM, Ian Kelly wrote: [...] >> Anyway, your point was to suggest that people would not be confused by >> having list multiplication copy lists but not other objects, because >> passing lists into functions as parameter

Re: Multi-dimensional list initialization

2012-11-07 Thread Steven D'Aprano
On Thu, 08 Nov 2012 00:30:53 +, Oscar Benjamin wrote: >> Every now and again I come across somebody who tries to distinguish >> between "call by foo" and "pass by foo", but nobody has been able to >> explain the difference (if any) to me. When you CALL a function, you >> PASS values to it. Hen

Re: Multi-dimensional list initialization

2012-11-07 Thread Andrew Robinson
On 11/07/2012 03:39 PM, Ian Kelly wrote: Why? Just to get rid of an FAQ? :-) Here's one of the more interesting uses from my own code: OK, and is this a main use case? (I'm not saying it isn't I'm asking.) Replacing the list multiplication in that function with a list comprehension would b

Re: Multi-dimensional list initialization

2012-11-07 Thread Greg Ewing
On 08/11/12 12:06, Oscar Benjamin wrote: On 7 November 2012 22:16, Joshua Landau wrote: That said, losing: [0] * (2, 3) == [0] * [2, 3] would mean losing duck-typing in general. There are precedents for this kind of thing; the string % operator treats tuples specially, for example. I don't t

Re: Multi-dimensional list initialization

2012-11-07 Thread Joshua Landau
On 7 November 2012 23:55, Andrew Robinson wrote: > On 11/07/2012 05:39 AM, Joshua Landau wrote: > > A more modest addition for the limited case described in this thread >> could be to use exponentiation: >> >> >>> [0] ** (2, 3) >> [[0, 0, 0], [0, 0, 0]] >> > I'm against over using the math ope

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On 8 November 2012 00:00, Steven D'Aprano wrote: > Andrew, it appears that your posts are being eaten or rejected by my > ISP's news server, because they aren't showing up for me. Possibly a side- > effect of your dates being in the distant past? So if you have replied to > any of my posts, I have

Re: Multi-dimensional list initialization

2012-11-07 Thread Andrew Robinson
On 11/07/2012 01:01 PM, Ian Kelly wrote: On Wed, Nov 7, 2012 at 12:51 PM, Andrew Robinson wrote: Interesting, you avoided the main point "lists are copied with list multiplication". It seems that each post is longer than the last. If we each responded to every point made, this thread would f

Re: Multi-dimensional list initialization

2012-11-07 Thread Steven D'Aprano
Andrew, it appears that your posts are being eaten or rejected by my ISP's news server, because they aren't showing up for me. Possibly a side- effect of your dates being in the distant past? So if you have replied to any of my posts, I haven't seen them. In any case, I wanted to ask a question:

Re: Multi-dimensional list initialization

2012-11-07 Thread Andrew Robinson
On 11/07/2012 05:39 AM, Joshua Landau wrote: On 7 November 2012 11:11, Oscar Benjamin wrote: On Nov 7, 2012 5:41 AM, "Gregory Ewing" wrote: > > If anything is to be done in this area, it would be better > as an extension of list comprehensions, e.g. > > [[None times

Re: Multi-dimensional list initialization

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 3:02 PM, Andrew Robinson wrote: > Draw up some use cases for the multiplication operator (I'm calling on your > experience, let's not trust mine, right?); What are all the Typical ways > people *Do* to use it now? > > If those use cases do not *primarily* center around *wan

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On 7 November 2012 22:16, Joshua Landau wrote: > On 7 November 2012 14:00, Oscar Benjamin wrote: >> On 7 November 2012 13:39, Joshua Landau >> wrote: >> > On 7 November 2012 11:11, Oscar Benjamin >> > wrote: >> >> A more modest addition for the limited case described in this thread >> >> could

Re: Multi-dimensional list initialization

2012-11-07 Thread Steven D'Aprano
On Wed, 07 Nov 2012 17:17:02 +, MRAB wrote: > The disadvantage of calling it "call by ..." is that it suggests that > you're just talking about calling functions. *shrug* There are already two synonyms for this, "call by ..." and "pass by ...". They are old, venerable terms dating back to A

Re: Multi-dimensional list initialization

2012-11-07 Thread Mark Lawrence
On 07/11/2012 22:02, Andrew Robinson wrote: You're doing extremely well, you've overtaken Xah Lee as the biggest waste of space on this list. -- Cheers. Mark Lawrence. -- http://mail.python.org/mailman/listinfo/python-list

Re: Multi-dimensional list initialization

2012-11-07 Thread Andrew Robinson
On 11/06/2012 10:56 PM, Demian Brecht wrote: My question was *not* based on what I perceive to be intuitive (although most of this thread has now seemed to devolve into that and become more of a philosophical debate), but was based on what I thought may have been inconsistent behaviour (which w

Re: Multi-dimensional list initialization

2012-11-07 Thread Joshua Landau
*Spoiler:* You've convinced me. On 7 November 2012 14:00, Oscar Benjamin wrote: > On 7 November 2012 13:39, Joshua Landau > wrote: > > On 7 November 2012 11:11, Oscar Benjamin > wrote: > >> A more modest addition for the limited case described in this thread > could > >> be to use exponentiati

Re: Multi-dimensional list initialization

2012-11-07 Thread Andrew Robinson
On 11/06/2012 05:55 PM, Steven D'Aprano wrote: On Tue, 06 Nov 2012 14:41:24 -0800, Andrew Robinson wrote: Yes. But this isn't going to cost any more time than figuring out whether or not the list multiplication is going to cause quirks, itself. Human psychology *tends* (it's a FAQ!) to autom

Re: Multi-dimensional list initialization

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 12:51 PM, Andrew Robinson wrote: > Interesting, you avoided the main point "lists are copied with list > multiplication". It seems that each post is longer than the last. If we each responded to every point made, this thread would fill a book. Anyway, your point was to su

Re: Multi-dimensional list initialization

2012-11-07 Thread Andrew Robinson
Hi IAN! On 11/06/2012 03:52 PM, Ian Kelly wrote: On Tue, Nov 6, 2012 at 3:41 PM, Andrew Robinson The objection is not nonsense; you've merely misconstrued it. If [[1,2,3]] * 4 is expected to create a mutable matrix of 1s, 2s, and 3s, then one would expect [[{}]] * 4 to create a mutable matrix

Re: Multi-dimensional list initialization

2012-11-07 Thread MRAB
On 2012-11-07 05:05, Steven D'Aprano wrote: On Wed, 07 Nov 2012 00:23:44 +, MRAB wrote: Incorrect. Python uses what is commonly known as call-by-object, not call-by-value or call-by-reference. Passing the list by value would imply that the list is copied, and that appends or removes to th

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On Nov 7, 2012 3:55 PM, "Ethan Furman" wrote: > > Oscar Benjamin wrote: >> >> A more modest addition for the limited case described in this thread could be to use exponentiation: >> >> >>> [0] ** (2, 3) >> [[0, 0, 0], [0, 0, 0]] > > > What would happen with > > --> [{}] ** (2, 3) The list being

RE: Multi-dimensional list initialization

2012-11-07 Thread Prasad, Ramit
Gregory Ewing wrote: > > Roy Smith wrote: > > Call by social network? The called function likes the object. > > Depending on how it feels, it can also comment on some of the object's > > attributes. > > And then finds that it has inadvertently shared all its > private data with other functions a

Re: Multi-dimensional list initialization

2012-11-07 Thread Ethan Furman
Oscar Benjamin wrote: On Nov 7, 2012 5:41 AM, "Gregory Ewing" > wrote: > > If anything is to be done in this area, it would be better > as an extension of list comprehensions, e.g. > > [[None times 5] times 10] > > which would be equivalent to > >

Re: Multi-dimensional list initialization

2012-11-07 Thread Ethan Furman
After this post the only credibility you have left (with me, anyway) is that you seem to be willing to learn. So learn the way Python works before you try to reimplement it. ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On 7 November 2012 13:39, Joshua Landau wrote: > > On 7 November 2012 11:11, Oscar Benjamin wrote: >> >> A more modest addition for the limited case described in this thread could >> be to use exponentiation: >> >> >>> [0] ** (2, 3) >> [[0, 0, 0], [0, 0, 0]] > > Hold on: why not just use multipli

Re: Multi-dimensional list initialization

2012-11-07 Thread Joshua Landau
On 7 November 2012 11:11, Oscar Benjamin wrote: > On Nov 7, 2012 5:41 AM, "Gregory Ewing" > wrote: > > > > If anything is to be done in this area, it would be better > > as an extension of list comprehensions, e.g. > > > > [[None times 5] times 10] > > > > which would be equivalent to > > > >

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On Nov 7, 2012 5:41 AM, "Gregory Ewing" wrote: > > If anything is to be done in this area, it would be better > as an extension of list comprehensions, e.g. > > [[None times 5] times 10] > > which would be equivalent to > > [[None for _i in xrange(5)] for _j in xrange(10)] I think you're righ

Re: Multi-dimensional list initialization

2012-11-07 Thread wxjmfauth
Le mercredi 7 novembre 2012 02:55:10 UTC+1, Steven D'Aprano a écrit : > > > > > > > Two-dimensional arrays in Python using lists are quite rare. Anyone who > > is doing serious numeric work where they need 2D arrays is using numpy, > > not lists. There are millions of people using Python

Re: Multi-dimensional list initialization

2012-11-07 Thread Jussi Piitulainen
Steven D'Aprano writes: > On Wed, 07 Nov 2012 00:23:44 +, MRAB wrote: > > I prefer the term "reference semantics". > > Oh good, because what the world needs is yet another name for the > same behaviour. > > - call by sharing > - call by object sharing > - call by object reference > - call by

Re: Multi-dimensional list initialization

2012-11-06 Thread Demian Brecht
On 2012-11-06, at 5:55 PM, Steven D'Aprano wrote: > I'm not entirely sure what your point is here. The OP screwed up -- he > didn't generate a 4-dimensional array. He generated a 2-dimensional > array. If his intuition about the number of dimensions is so poor, why > should his intuition abou

Re: Multi-dimensional list initialization

2012-11-06 Thread Gregory Ewing
If anything is to be done in this area, it would be better as an extension of list comprehensions, e.g. [[None times 5] times 10] which would be equivalent to [[None for _i in xrange(5)] for _j in xrange(10)] -- Greg -- http://mail.python.org/mailman/listinfo/python-list

Re: Multi-dimensional list initialization

2012-11-06 Thread Gregory Ewing
Roy Smith wrote: Call by social network? The called function likes the object. Depending on how it feels, it can also comment on some of the object's attributes. And then finds that it has inadvertently shared all its private data with other functions accessing the object. -- Greg -- http:/

Re: Multi-dimensional list initialization

2012-11-06 Thread Roy Smith
In article <5099ec1d$0$21759$c3e8da3$76491...@news.astraweb.com>, Steven D'Aprano wrote: > On Wed, 07 Nov 2012 00:23:44 +, MRAB wrote: > > >> Incorrect. Python uses what is commonly known as call-by-object, not > >> call-by-value or call-by-reference. Passing the list by value would > >>

Re: Multi-dimensional list initialization

2012-11-06 Thread Steven D'Aprano
On Wed, 07 Nov 2012 00:23:44 +, MRAB wrote: >> Incorrect. Python uses what is commonly known as call-by-object, not >> call-by-value or call-by-reference. Passing the list by value would >> imply that the list is copied, and that appends or removes to the list >> inside the function would no

Re: Multi-dimensional list initialization

2012-11-06 Thread rusi
On Nov 7, 5:26 am, MRAB wrote: > I prefer the term "reference semantics". Ha! That hits the nail on the head. To go back to the OP: On Nov 5, 11:28 am, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D > matrix" (running 2.7.3, non-core libs not a

Re: Multi-dimensional list initialization

2012-11-06 Thread Steven D'Aprano
On Tue, 06 Nov 2012 14:41:24 -0800, Andrew Robinson wrote: > Yes. But this isn't going to cost any more time than figuring out > whether or not the list multiplication is going to cause quirks, itself. > Human psychology *tends* (it's a FAQ!) to automatically assume the > purpose of the list mul

Re: Multi-dimensional list initialization

2012-11-06 Thread MRAB
On 2012-11-06 23:52, Ian Kelly wrote: On Tue, Nov 6, 2012 at 3:41 PM, Andrew Robinson wrote: Q: What about other mutable objects like sets or dicts? A: No, the elements are never copied. They aren't list multiplication compatible in any event! It's a total nonsense objection. If th

Re: Multi-dimensional list initialization

2012-11-06 Thread Ian Kelly
On Tue, Nov 6, 2012 at 3:41 PM, Andrew Robinson wrote: >> Q: What about other mutable objects like sets or dicts? >> A: No, the elements are never copied. > > They aren't list multiplication compatible in any event! It's a total > nonsense objection. > > If these are inconsistent in my i

RE: Multi-dimensional list initialization

2012-11-06 Thread Prasad, Ramit
Andrew Robinson wrote: > > On 11/06/2012 01:04 AM, Steven D'Aprano wrote: > > On Mon, 05 Nov 2012 21:51:24 -0800, Andrew Robinson wrote: > > [snip] > > Q: What about other mutable objects like sets or dicts? > > A: No, the elements are never copied. > They aren't list multiplication comp

Re: Multi-dimensional list initialization

2012-11-06 Thread Andrew Robinson
On 11/06/2012 01:04 AM, Steven D'Aprano wrote: On Mon, 05 Nov 2012 21:51:24 -0800, Andrew Robinson wrote: The most compact notation in programming really ought to reflect the most *commonly* desired operation. Otherwise, we're really just making people do extra typing for no reason. There are

Re: Multi-dimensional list initialization

2012-11-06 Thread Ian Kelly
On Tue, Nov 6, 2012 at 2:36 PM, Andrew Robinson wrote: > I meant all lists are shallow copied from the innermost level out. > Equivalently, it's a deep copy of list objects -- but a shallow copy of any > list contents except other lists. Why only list objects, though? When a user writes [[]] *

Re: Multi-dimensional list initialization

2012-11-06 Thread Andrew Robinson
On 11/06/2012 01:19 AM, Ian Kelly wrote: On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson If you nest it another time; [[[None]]]*4, the same would happen; all lists would be independent -- but the objects which aren't lists would be refrenced-- not copied. a=[[["alpha","beta"]]]*4 would yield:

Re: Multi-dimensional list initialization

2012-11-06 Thread Andrew Robinson
On 11/06/2012 09:32 AM, Prasad, Ramit wrote: Ian Kelly wrote: On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson [snip] See if you can find *any* python program where people desired the multiplication to have the die effect that changing an object in one of the sub lists -- changes all the objec

Re: Multi-dimensional list initialization

2012-11-06 Thread Andrew Robinson
On 11/06/2012 06:35 AM, Oscar Benjamin wrote: > In general, people don't use element multiplication (that I have *ever* seen) to make lists where all elements of the outer most list point to the same sub-*list* by reference. The most common use of the multiplication is to fill an array with

RE: Multi-dimensional list initialization

2012-11-06 Thread Prasad, Ramit
Ian Kelly wrote: > > On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson > [snip] > > See if you can find *any* python program where people desired the > > multiplication to have the die effect that changing an object in one of the > > sub lists -- changes all the objects in the other sub lists. > >

Re: Multi-dimensional list initialization

2012-11-06 Thread Oscar Benjamin
On Nov 6, 2012 6:00 AM, "Andrew Robinson" wrote: > > On 11/05/2012 06:30 PM, Oscar Benjamin wrote: >> >> stuff = [[obj] * n] * m >> >> I thought that the multiplication of the inner list ([obj] * n) by m >> could create a new list of lists using copies. On closer inspection I >> see that the l

RE: Multi-dimensional list initialization

2012-11-06 Thread Shambhu Rajak
Well said Steve, I agree with you... -Shambhu -Original Message- From: Steven D'Aprano [mailto:steve+comp.lang.pyt...@pearwood.info] Sent: Tuesday, November 06, 2012 2:35 PM To: python-list@python.org Subject: Re: Multi-dimensional list initialization On Mon, 05 Nov 2012 21:51:24

Re: Multi-dimensional list initialization

2012-11-06 Thread Ian Kelly
On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson wrote: > If you nest it another time; > [[[None]]]*4, the same would happen; all lists would be independent -- but > the objects which aren't lists would be refrenced-- not copied. > > a=[[["alpha","beta"]]]*4 would yield: > a=[[['alpha', 'beta']], [

Re: Multi-dimensional list initialization

2012-11-06 Thread Steven D'Aprano
On Mon, 05 Nov 2012 21:51:24 -0800, Andrew Robinson wrote: > The most compact notation in programming really ought to reflect the > most *commonly* desired operation. Otherwise, we're really just making > people do extra typing for no reason. There are many reasons not to put minimizing of typin

Re: Multi-dimensional list initialization

2012-11-06 Thread Andrew Robinson
On 11/05/2012 10:07 PM, Chris Angelico wrote: On Tue, Nov 6, 2012 at 4:51 PM, Andrew Robinson wrote: I really don't think doing a shallow copy of lists would break anyone's program. Well, it's a change, a semantic change. It's almost certainly going to break _something_. But for the sake of a

Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 4:51 PM, Andrew Robinson wrote: > I really don't think doing a shallow copy of lists would break anyone's > program. Well, it's a change, a semantic change. It's almost certainly going to break _something_. But for the sake of argument, we can suppose that the change could

Re: Multi-dimensional list initialization

2012-11-05 Thread Andrew Robinson
On 11/05/2012 06:30 PM, Oscar Benjamin wrote: On 6 November 2012 02:01, Chris Angelico wrote: On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin wrote: I was just thinking to myself that it would be a hard thing to change because the list would need to know how to instantiate copies of all the

Re: Multi-dimensional list initialization

2012-11-05 Thread Oscar Benjamin
On 6 November 2012 02:01, Chris Angelico wrote: > On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin > wrote: >> I was just thinking to myself that it would be a hard thing to change >> because the list would need to know how to instantiate copies of all >> the different types of the elements in the

Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin wrote: > I was just thinking to myself that it would be a hard thing to change > because the list would need to know how to instantiate copies of all > the different types of the elements in the list. Then I realised it > doesn't. It is simply a case

Re: Multi-dimensional list initialization

2012-11-05 Thread Oscar Benjamin
On 5 November 2012 09:13, Hans Mulder wrote: > On 5/11/12 07:27:52, Demian Brecht wrote: >> So, here I was thinking "oh, this is a nice, easy way to initialize a 4D >> matrix" >> (running 2.7.3, non-core libs not allowed): >> >> m = [[None] * 4] * 4 >> >> The way to get what I was after was: >> >

Re: Multi-dimensional list initialization

2012-11-05 Thread Joshua Landau
On 5 November 2012 06:27, Demian Brecht wrote: > >>> a = [None] * 4 > >>> a[0] = 'a' > >>> a > ['a', None, None, None] > > >>> m = [[None] * 4] * 4 > >>> m[0][0] = 'm' > >>> m > [['m', None, None, None], ['m', None, None, None], ['m', None, None, > None], ['m', None, None, None]] > > Is this expe

Re: Multi-dimensional list initialization

2012-11-05 Thread Demian Brecht
On 2012-11-04, at 11:07 PM, Chris Rebert wrote: > However, unlike a list object (as in your latter example), the object > `None` is completely immutable (and what's more, a singleton value), > so you just-so-happen *not to be able to* run into the same problem of > mutating an object (assignment

Re: Multi-dimensional list initialization

2012-11-05 Thread Demian Brecht
On 2012-11-04, at 10:44 PM, Andrew Robinson wrote: > but I think you meant: > > m = [[None] * 4, [None] * 4, [None] * 4, [None] *4 ] > rather than: > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] Yes, I meant the former, thanks for catching the typo. Demian Brecht @demianbrecht http://d

Re: Multi-dimensional list initialization

2012-11-05 Thread wxjmfauth
Le lundi 5 novembre 2012 07:28:00 UTC+1, Demian Brecht a écrit : > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D > matrix" (running 2.7.3, non-core libs not allowed): > > > > m = [[None] * 4] * 4 > > > > The way to get what I was after was: > > > > m = [[None] *

Re: Multi-dimensional list initialization

2012-11-05 Thread Hans Mulder
On 5/11/12 07:27:52, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D > matrix" > (running 2.7.3, non-core libs not allowed): > > m = [[None] * 4] * 4 > > The way to get what I was after was: > > m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]]

Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Mon, Nov 5, 2012 at 6:54 PM, Andrew Robinson wrote: > On 11/04/2012 11:27 PM, Chris Angelico wrote: >> >> On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebert wrote: >> >> x = None >> x.a = 42 >>> >>> Traceback (most recent call last): >>>File "", line 1, in >>> AttributeError: 'NoneTy

Re: Multi-dimensional list initialization

2012-11-04 Thread Andrew Robinson
On 11/04/2012 11:27 PM, Chris Angelico wrote: On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebert wrote: x = None x.a = 42 Traceback (most recent call last): File "", line 1, in AttributeError: 'NoneType' object has no attribute 'a' Python needs a YouGottaBeKiddingMeError for times when you do so

Re: Multi-dimensional list initialization

2012-11-04 Thread Chris Angelico
On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebert wrote: x = None x.a = 42 > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'NoneType' object has no attribute 'a' Python needs a YouGottaBeKiddingMeError for times when you do something utterly insane like this.

Re: Multi-dimensional list initialization

2012-11-04 Thread Chris Rebert
On Sun, Nov 4, 2012 at 10:27 PM, Demian Brecht wrote: > So, here I was thinking "oh, this is a nice, easy way to initialize a 4D > matrix" (running 2.7.3, non-core libs not allowed): > > m = [[None] * 4] * 4 > > The way to get what I was after was: > > m = [[None] * 4, [None] * 4, [None] * 4, [No

Re: Multi-dimensional list initialization

2012-11-04 Thread Andrew Robinson
On 11/04/2012 10:27 PM, Demian Brecht wrote: So, here I was thinking "oh, this is a nice, easy way to initialize a 4D matrix" (running 2.7.3, non-core libs not allowed): m = [[None] * 4] * 4 The way to get what I was after was: m = [[None] * 4, [None] * 4, [None] * 4, [None * 4]] FYI: The b

Re: Multi-dimensional list initialization trouble

2006-05-25 Thread trebucket
An expression like this creates a list of integers: >>> [0] * 2 [0, 0] But an expression like this creates list of references to the list named `foo': >>> foo = [0, 0] >>> baz = [foo] * 2 [foo, foo] So, setting baz[0][0] = 1, is really setting foo[0] = 1. There is only one instance of foo, but y

Re: Multi-dimensional list initialization trouble

2006-05-25 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > Hello I found this very strange; is it a bug, is it a "feature", am I > being naughty or what? the repeat operator (*) creates a new list with references to the same inner objects, so you end up with a list containing multiple references to the same list. also see:

Re: Multi-dimensional list initialization trouble

2006-05-25 Thread Scott David Daniels
[EMAIL PROTECTED] wrote: > Hello I found this very strange; is it a bug, is it a "feature", am I > being naughty or what? > foo = [[0, 0], [0, 0]] baz = [ [0]*2 ] * 2 >... > Why on earth does foo and baz behave differently?? This is a frequently made mistake. try also: >>> bumble = [[0