Re: Multi-dimensional list initialization

2012-11-09 Thread rusi
On Nov 9, 11:37 am, Steven D'Aprano steve +comp.lang.pyt...@pearwood.info wrote: On Fri, 09 Nov 2012 17:07:09 +1100, Chris Angelico wrote: On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence breamore...@yahoo.co.uk wrote: On 07/11/2012 01:55, Steven D'Aprano wrote: Who knows? Who cares?

Re: Multi-dimensional list initialization

2012-11-09 Thread Chris Angelico
On Sat, Nov 10, 2012 at 2:05 AM, rusi rustompm...@gmail.com 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 --

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 ros...@gmail.com declaimed the following in gmane.comp.python.general: On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence breamore...@yahoo.co.uk wrote: On 07/11/2012 01:55, Steven D'Aprano wrote: Who

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~ --

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 andr...@r3dsolutions.com 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

Re: Multi-dimensional list initialization

2012-11-08 Thread wrw
On Nov 7, 2012, at 11:51 PM, Andrew Robinson andr...@r3dsolutions.com 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

Re: Multi-dimensional list initialization

2012-11-08 Thread Ian Kelly
On Thu, Nov 8, 2012 at 1:26 AM, Andrew Robinson andr...@r3dsolutions.com 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

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 demianbre...@gmail.com 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]

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 Chris Angelico
On Fri, Nov 9, 2012 at 12:39 PM, Mark Lawrence breamore...@yahoo.co.uk 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 :=

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 breamore...@yahoo.co.uk 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

Re: Multi-dimensional list initialization

2012-11-08 Thread Chris Angelico
On Fri, Nov 9, 2012 at 5:37 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 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

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 breamore...@yahoo.co.uk 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-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 object -

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, so it's

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On Nov 7, 2012 5:41 AM, Gregory Ewing greg.ew...@canterbury.ac.nz 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

Re: Multi-dimensional list initialization

2012-11-07 Thread Joshua Landau
On 7 November 2012 11:11, Oscar Benjamin oscar.j.benja...@gmail.com wrote: On Nov 7, 2012 5:41 AM, Gregory Ewing greg.ew...@canterbury.ac.nz 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]

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On 7 November 2012 13:39, Joshua Landau joshua.landau...@gmail.com wrote: On 7 November 2012 11:11, Oscar Benjamin oscar.j.benja...@gmail.com 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]]

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 Ethan Furman
Oscar Benjamin wrote: On Nov 7, 2012 5:41 AM, Gregory Ewing greg.ew...@canterbury.ac.nz mailto:greg.ew...@canterbury.ac.nz 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

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 accessing

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On Nov 7, 2012 3:55 PM, Ethan Furman et...@stoneleaf.us 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 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

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 Ian Kelly
On Wed, Nov 7, 2012 at 12:51 PM, Andrew Robinson andr...@r3dsolutions.com 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,

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

Re: Multi-dimensional list initialization

2012-11-07 Thread Joshua Landau
*Spoiler:* You've convinced me. On 7 November 2012 14:00, Oscar Benjamin oscar.j.benja...@gmail.com wrote: On 7 November 2012 13:39, Joshua Landau joshua.landau...@gmail.com wrote: On 7 November 2012 11:11, Oscar Benjamin oscar.j.benja...@gmail.com wrote: A more modest addition for the

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

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 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 Algol

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On 7 November 2012 22:16, Joshua Landau joshua.landau...@gmail.com wrote: On 7 November 2012 14:00, Oscar Benjamin oscar.j.benja...@gmail.com wrote: On 7 November 2012 13:39, Joshua Landau joshua.landau...@gmail.com wrote: On 7 November 2012 11:11, Oscar Benjamin oscar.j.benja...@gmail.com

Re: Multi-dimensional list initialization

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 3:02 PM, Andrew Robinson andr...@r3dsolutions.com 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

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 5]

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

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 andr...@r3dsolutions.com 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

Re: Multi-dimensional list initialization

2012-11-07 Thread Oscar Benjamin
On 8 November 2012 00:00, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 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

Re: Multi-dimensional list initialization

2012-11-07 Thread Joshua Landau
On 7 November 2012 23:55, Andrew Robinson andr...@r3dsolutions.com 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

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 Landaujoshua.landau...@gmail.com 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

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

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. Hence the two

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 parameters

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

Re: Multi-dimensional list initialization

2012-11-07 Thread Ian Kelly
On Wed, Nov 7, 2012 at 8:13 PM, Andrew Robinson andr...@r3dsolutions.com 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

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 andr...@r3dsolutions.com 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

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 typing

Re: Multi-dimensional list initialization

2012-11-06 Thread Ian Kelly
On Tue, Nov 6, 2012 at 1:21 AM, Andrew Robinson andr...@r3dsolutions.com 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:

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 -0800

Re: Multi-dimensional list initialization

2012-11-06 Thread Oscar Benjamin
On Nov 6, 2012 6:00 AM, Andrew Robinson andr...@r3dsolutions.com 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

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. I'm sure

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 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

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 Ian Kelly
On Tue, Nov 6, 2012 at 2:36 PM, Andrew Robinson andr...@r3dsolutions.com 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

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

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 compatible in

Re: Multi-dimensional list initialization

2012-11-06 Thread Ian Kelly
On Tue, Nov 6, 2012 at 3:41 PM, Andrew Robinson andr...@r3dsolutions.com 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

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 andr...@r3dsolutions.com 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

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

Re: Multi-dimensional list initialization

2012-11-06 Thread rusi
On Nov 7, 5:26 am, MRAB pyt...@mrabarnett.plus.com 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 demianbre...@gmail.com wrote: So, here I was thinking oh, this is a nice, easy way to initialize a 4D

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 not

Re: Multi-dimensional list initialization

2012-11-06 Thread Roy Smith
In article 5099ec1d$0$21759$c3e8da3$76491...@news.astraweb.com, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 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

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 --

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 Demian Brecht
On 2012-11-06, at 5:55 PM, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info 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

Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Mon, Nov 5, 2012 at 6:54 PM, Andrew Robinson andr...@r3dsolutions.com wrote: On 11/04/2012 11:27 PM, Chris Angelico wrote: On Mon, Nov 5, 2012 at 6:07 PM, Chris Rebertc...@rebertia.com wrote: x = None x.a = 42 Traceback (most recent call last): File stdin, line 1, inmodule

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]] Or

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] * 4, [None] * 4,

Re: Multi-dimensional list initialization

2012-11-05 Thread Demian Brecht
On 2012-11-04, at 10:44 PM, Andrew Robinson andr...@r3dsolutions.com 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

Re: Multi-dimensional list initialization

2012-11-05 Thread Demian Brecht
On 2012-11-04, at 11:07 PM, Chris Rebert c...@rebertia.com 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

Re: Multi-dimensional list initialization

2012-11-05 Thread Joshua Landau
On 5 November 2012 06:27, Demian Brecht demianbre...@gmail.com 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 expected

Re: Multi-dimensional list initialization

2012-11-05 Thread Oscar Benjamin
On 5 November 2012 09:13, Hans Mulder han...@xs4all.nl 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: m

Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin oscar.j.benja...@gmail.com 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

Re: Multi-dimensional list initialization

2012-11-05 Thread Oscar Benjamin
On 6 November 2012 02:01, Chris Angelico ros...@gmail.com wrote: On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin oscar.j.benja...@gmail.com 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 Andrew Robinson
On 11/05/2012 06:30 PM, Oscar Benjamin wrote: On 6 November 2012 02:01, Chris Angelicoros...@gmail.com wrote: On Tue, Nov 6, 2012 at 12:32 PM, Oscar Benjamin oscar.j.benja...@gmail.com wrote: I was just thinking to myself that it would be a hard thing to change because the list would need to

Re: Multi-dimensional list initialization

2012-11-05 Thread Chris Angelico
On Tue, Nov 6, 2012 at 4:51 PM, Andrew Robinson andr...@r3dsolutions.com 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

Multi-dimensional list initialization

2012-11-04 Thread Demian Brecht
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]] (Obviously, I could have just hardcoded the

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

Re: Multi-dimensional list initialization

2012-11-04 Thread Chris Rebert
On Sun, Nov 4, 2012 at 10:27 PM, Demian Brecht demianbre...@gmail.com 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,

Re: Multi-dimensional list initialization

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

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 Rebertc...@rebertia.com wrote: x = None x.a = 42 Traceback (most recent call last): File stdin, line 1, inmodule AttributeError: 'NoneType' object has no attribute 'a' Python needs a

Multi-dimensional list initialization trouble

2006-05-25 Thread jonkje
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 foo [[0, 0], [0, 0]] baz [[0, 0], [0, 0]] foo[0][0]=1 baz[0][0]=1 foo [[1, 0], [0, 0]] baz [[1, 0], [1, 0]] Why on earth does foo and baz behave

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]*2 for 0 in

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 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 you have