[Tutor] Not understanding a bit of code behavior

Alan Gauld alan.gauld at btinternet.com
Tue Jan 25 08:42:52 CET 2011


"Bill Allen" <wallenpb at gmail.com> wrote

> I am convinced that I had previously assigned part_list to out_list 
> by
> reference, not value as I mistaken thought when I first wrote the 
> code,

In Python variables are names which refer to objects.
So your assignment made both names refer to the same object.

> Question, is it possible to copy values from one object to another 
> in such a
> way as they are not just references one to the other?

If you want to copy a list the usual way is to create a new copy
using slicing:

newList = oldList[:]

You can also use the deepcopy module if the list is multi level.

> Sorry about asking questions and then answering them.  Things began 
> to
> become more clear with each question I asked.

Thats often the way :-)
The act of articulating the problem forces you to think about it 
differently.

HTH

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list