Re: initialized list: strange behavior

2008-11-25 Thread Arnaud Delobelle
Jason Scheirer [EMAIL PROTECTED] writes:

 On Nov 24, 10:34 pm, [EMAIL PROTECTED] wrote:
 Hi Python experts! Please explain this behavior:

  nn=3*[[]]
  nn
 [[], [], []]
  mm=[[],[],[]]
  mm

 [[], [], []]

 Up till now, 'mm' and 'nn' look the same, right? Nope!

  mm[1].append(17)
  mm
 [[], [17], []]
  nn[1].append(17)
  nn

 [[17], [17], [17]]

 ???

 Python 2.5 Win XP

 Thanks!

 You're creating three references to the same list with the
 multiplication operator.

There's no need to introduce references: you're creating a list with the
same object at each position.

[...]
 Python is pass-by-reference, not pass-by-value.

It's certainly not pass-by-reference, nor is it pass-by-value IMHO.

-- 
Arnaud
--
http://mail.python.org/mailman/listinfo/python-list


Re: initialized list: strange behavior

2008-11-25 Thread Benjamin Kaplan
On Tue, Nov 25, 2008 at 9:23 AM, Arnaud Delobelle [EMAIL PROTECTED]wrote:

 Jason Scheirer [EMAIL PROTECTED] writes:

  On Nov 24, 10:34 pm, [EMAIL PROTECTED] wrote:
  Hi Python experts! Please explain this behavior:
 
   nn=3*[[]]
   nn
  [[], [], []]
   mm=[[],[],[]]
   mm
 
  [[], [], []]
 
  Up till now, 'mm' and 'nn' look the same, right? Nope!
 
   mm[1].append(17)
   mm
  [[], [17], []]
   nn[1].append(17)
   nn
 
  [[17], [17], [17]]
 
  ???
 
  Python 2.5 Win XP
 
  Thanks!
 
  You're creating three references to the same list with the
  multiplication operator.

 There's no need to introduce references: you're creating a list with the
 same object at each position.

 [...]
  Python is pass-by-reference, not pass-by-value.

 It's certainly not pass-by-reference, nor is it pass-by-value IMHO.


Please don't get into this here. We have enough threads for this already.




 --
 Arnaud
 --
 http://mail.python.org/mailman/listinfo/python-list

--
http://mail.python.org/mailman/listinfo/python-list


Re: initialized list: strange behavior

2008-11-25 Thread Steve Holden
Arnaud Delobelle wrote:
 Jason Scheirer [EMAIL PROTECTED] writes:
 
 On Nov 24, 10:34 pm, [EMAIL PROTECTED] wrote:
 Hi Python experts! Please explain this behavior:

 nn=3*[[]]
 nn
 [[], [], []]
 mm=[[],[],[]]
 mm
 [[], [], []]

 Up till now, 'mm' and 'nn' look the same, right? Nope!

 mm[1].append(17)
 mm
 [[], [17], []]
 nn[1].append(17)
 nn
 [[17], [17], [17]]

 ???

 Python 2.5 Win XP

 Thanks!
 You're creating three references to the same list with the
 multiplication operator.
 
 There's no need to introduce references: you're creating a list with the
 same object at each position.
 
 [...]
 Python is pass-by-reference, not pass-by-value.
 
 It's certainly not pass-by-reference, nor is it pass-by-value IMHO.
 
Since no lists are being passed as arguments in these examples it's not
pass-by-anything. Jump off that horse right now!

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

--
http://mail.python.org/mailman/listinfo/python-list


Re: initialized list: strange behavior

2008-11-25 Thread alexander . genkin
The issue is exhausted in Python Library Reference, Chapter 3.6, so I
should apologize for initial posting. All comments were helpful,
though Arnaud and Steve are right that pass-by-anything is off the
point.

Thanks All!

--
http://mail.python.org/mailman/listinfo/python-list


Re: initialized list: strange behavior

2008-11-25 Thread Arnaud Delobelle
Steve Holden [EMAIL PROTECTED] writes:

 Arnaud Delobelle wrote:
 Jason Scheirer [EMAIL PROTECTED] writes:
 Python is pass-by-reference, not pass-by-value.
 
 It's certainly not pass-by-reference, nor is it pass-by-value IMHO.
 
 Since no lists are being passed as arguments in these examples it's not
 pass-by-anything. Jump off that horse right now!

You're right that Python's calling semantics have nothing to do with the
question asked.  I just thought best not to leave the OP under any
misapprehension.

-- 
Arnaud
--
http://mail.python.org/mailman/listinfo/python-list


initialized list: strange behavior

2008-11-24 Thread alexander . genkin
Hi Python experts! Please explain this behavior:

 nn=3*[[]]
 nn
[[], [], []]
 mm=[[],[],[]]
 mm
[[], [], []]

Up till now, 'mm' and 'nn' look the same, right? Nope!

 mm[1].append(17)
 mm
[[], [17], []]
 nn[1].append(17)
 nn
[[17], [17], [17]]

???

Python 2.5 Win XP

Thanks!
--
http://mail.python.org/mailman/listinfo/python-list


Re: initialized list: strange behavior

2008-11-24 Thread Gary Herron
[EMAIL PROTECTED] wrote:
 Hi Python experts! Please explain this behavior:
   

[] make an empty list.
[ [],[],[] ] makes a list of three different empty lists.
3*[[]]  makes a list of three references to the same list.

Realy, that should explain it all, but perhaps there are enough empty
lists here to obscure things.  Try this:

Here b is a list that contains three references to a.   Modify a, and
all three references to a show the modification:

 a = [1,2,3]
 b = [a,a,a]
 a.append(4)
 b
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]

Gary Herron



   
 nn=3*[[]]
 nn
 
 [[], [], []]
   
 mm=[[],[],[]]
 mm
 
 [[], [], []]

 Up till now, 'mm' and 'nn' look the same, right? Nope!

   
 mm[1].append(17)
 mm
 
 [[], [17], []]
   
 nn[1].append(17)
 nn
 
 [[17], [17], [17]]

 ???

 Python 2.5 Win XP

 Thanks!
 --
 http://mail.python.org/mailman/listinfo/python-list
   

--
http://mail.python.org/mailman/listinfo/python-list


Re: initialized list: strange behavior

2008-11-24 Thread Jason Scheirer
On Nov 24, 10:34 pm, [EMAIL PROTECTED] wrote:
 Hi Python experts! Please explain this behavior:

  nn=3*[[]]
  nn
 [[], [], []]
  mm=[[],[],[]]
  mm

 [[], [], []]

 Up till now, 'mm' and 'nn' look the same, right? Nope!

  mm[1].append(17)
  mm
 [[], [17], []]
  nn[1].append(17)
  nn

 [[17], [17], [17]]

 ???

 Python 2.5 Win XP

 Thanks!

You're creating three references to the same list with the
multiplication operator. You can easily get the same behavior because
of similar mechanics in a more common scenario:

In [1]: a = []

In [2]: b = a

In [3]: a, b
Out[3]: ([], [])

In [4]: a.append(100)

In [5]: a, b
Out[5]: ([100], [100])

Python is pass-by-reference, not pass-by-value.
--
http://mail.python.org/mailman/listinfo/python-list