[Python-bugs-list] [ python-Bugs-422669 ] Bug with appending objects to list.

noreply@sourceforge.net noreply@sourceforge.net
Wed, 09 May 2001 14:06:49 -0700


Bugs item #422669, was updated on 2001-05-09 08:17
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=422669&group_id=5470

Category: Python Interpreter Core
Group: None
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Pavel Khmelinsky (russhmepas)
>Assigned to: Nobody/Anonymous (nobody)
Summary: Bug with appending objects to list.

Initial Comment:
Please, excuse me if this problem is problem of my brain and not a bug....

1st source:
*******************
>>> class cl:
... 	i = 0
...	def grown(self):
...		self.i = self.i + 1
...
...c = cl()
...db = []
...j = 0
...for i in range(0, 10):
...	c.grown()
...	db.append(c)
...	print vars( db[j] )
...	j = j + 1
{'i': 1}
{'i': 2}
{'i': 3}
{'i': 4}
{'i': 5}
{'i': 6}
{'i': 7}
{'i': 8}
{'i': 9}
{'i': 10}
*****************

All rights!

Lets try to see whats the members of the list "db" after end of cycle:

*****************
>>> class cl:
... 	i = 0
... 	def grown(self):
... 		self.i = self.i + 1
... 
... c = cl()
... db = []
... for i in range(0, 10):
... 	c.grown()
... 	db.append(c)
... 
... for j in db:
... 	print vars(j)
{'i': 10}
{'i': 10}
{'i': 10}
{'i': 10}
{'i': 10}
{'i': 10}
{'i': 10}
{'i': 10}
{'i': 10}
{'i': 10}
********************

Why!? Why all members of list are similiar?
This problem doesn'n exist if I appending to list a sipmle type variable (like integer), not
a class object.

P.S.: Sorry for my English, my 1st language is Russian. 
P.P.S.: I am using W2k professional 

Regards,
Pavel Khmelinsky

----------------------------------------------------------------------

>Comment By: Pavel Khmelinsky (russhmepas)
Date: 2001-05-09 14:06

Message:
Logged In: YES 
user_id=215002

Please, sorry for my nonsense.
Now I understand what it is not bug.
If somebody interesting the right code is:
*************
>>> import copy
... class cl:
...     i = 0
...     def grown(self):
...         self.i = self.i + 1
... 
... c = cl()
... db = []
... for i in range(0, 10):
...     c.grown()
...     db.append(copy.deepcopy(c))
... 
... for j in db:
...     print vars(j)
{'i': 1}
{'i': 2}
{'i': 3}
{'i': 4}
{'i': 5}
{'i': 6}
{'i': 7}
{'i': 8}
{'i': 9}
{'i': 10}
***************

Big thanks for Jeremy. (Words "name binding" and "mutable" very helps me)


----------------------------------------------------------------------

Comment By: Pavel Khmelinsky (russhmepas)
Date: 2001-05-09 10:19

Message:
Logged In: YES 
user_id=215002

jhylton,
Why? Why "same"? Yes, same object, but with another properties. Before append I called "grow()" method, 
which change "i" variable.
May be list db contain not object, but simple point (link) to object in memory? But I append not point! I 
append Object, so object must be copied to list, isn't it?
If when I append something to list, appending point instead object, why this example
*********************
>>> m=[]
... for j in range(0,10):
...     m.append(j)
... 
... for k in range(0,10):
...     print m[k]
0
1
2
3
4
5
6
7
8
9
******************

working right?

P.S.: I am using python 1.5.2
Thank you for you help!
BG,
Pavel Khmelinsky

----------------------------------------------------------------------

Comment By: Jeremy Hylton (jhylton)
Date: 2001-05-09 08:37

Message:
Logged In: YES 
user_id=31392

In the second example, you've append the *same object* to
the list
each time. 


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=422669&group_id=5470