scared about refrences...

Fredrik Lundh fredrik at pythonware.com
Mon Oct 30 14:39:35 EST 2006


SpreadTooThin wrote:

> I'm really worried that python may is doing some things I wasn't
> expecting... but lets see...
> 
> if I pass a list to a function def fn(myList):
> 
> and in that function I modify an element in the list, then does the
> callers list get modied as well.
> 
> def fn(list):
>    list[1] = 0
> 
> myList = [1, 2, 3]
> print myList
> fn(myList)
> print myList
> 
>>>> [1,2,3]
>>>> [1,0,3]
> 
> How can I avoid this?

by not modifying your arguments nilly-willy, of course.

> How do I specify or create deep copies of objects that may contain
> other objects that may contain other
> object that may contain other objects....

http://www.effbot.org/pyfaq/how-do-i-copy-an-object-in-python.htm

but if you find yourself needing to copy things a lot, you need to look 
over your design.  well-designed Python code seldom needs to copy object
values.

</F>




More information about the Python-list mailing list