scared about refrences...

John Henry john106henry at hotmail.com
Mon Oct 30 14:35:47 EST 2006


I am no Python guru - just an ordinary user.

There is nothing "scary" about this.  There are (many) situations where
this is actually *desirable* but of course there are (many) situations
where this is an unwelcomed side-effect.

In situations where I don't want this to happen, I simply pass down the
list as an non-mutable object (like converting the list to a tuple).

It took me a little bit of getting used to this concept as well:
everything is either a mutable object, or a non-mutable object.  I just
have to throw away trying to use concept of "pointers" in Python.

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?  In this case this is a really simplified example
> but the effects are the same...
> 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....




More information about the Python-list mailing list