Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
I don't understand what this means
self.append(copy.copy(value)) mean. I understand that self.append adds whatever is added to the end of the list but what does the rest of it mean?
Chris FreemanTreehouse Moderator 68,227 Points
So you’ve got the part how
would add the object
value to end of the list. It’s interesting that Kenneth chose the variable name “value” since this is closer to the truth. In Python, a
list contains only the reference ID value to each object in the list and not the actual object. The built-in function
id returns the unique id of an object (which is its location address in memory).
>>> item = [1, 2, 3] >>> l =  >>> for _ in range(3): ... l.append(item) ... >>> for obj in l: ... print(id(obj)) ... 4717438664 4717438664 4717438664
Each item in the list is the same item since they all have the same id.
copy, a new replicated version is created separate from the original.
>>> for _ in range(3): ... l.append(copy.copy(item)) ... >>> for obj in l: ... print(id(obj)) ... 4711909320 4711910600 4711915592 >>> l = 
Now, if the original object is changed, none of the copies will be affected.
Bonus: an immutable object is not copied by
copy. Since the an immutable object can’t change you don’t need more than one instance of it.
>>> obj = 'immutable object' >>> obj == copy.copy(obj) True >>> id(obj) 4717502376 >>> id(copy.copy(obj)) 4717502376
Post back if you need more help. Good luck!!!