scared about refrences...

SpreadTooThin bjobrien62 at gmail.com
Mon Oct 30 15:13:21 EST 2006


Marc 'BlackJack' Rintsch wrote:
> In <1162236136.367603.165180 at b28g2000cwb.googlegroups.com>, SpreadTooThin
> wrote:
>
> > I'm really worried that python may is doing some things I wasn't
> > expecting... but lets see...
>
> Expect that Python never copies something if don't ask explicitly for a
> copy.
>
> > 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...
>
> In this case:
>
> def fn(lst):
>     lst = list(lst)
>     lst[1] = 0
>
>
> > 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....
>
> See the `copy` module especially `copy.deepcopy()`.
>
I'm aware of __deepcopy__ but does that mean that every object in the
object
needs to have its own deep copy?  And how do I ensure that?




> Ciao,
> 	Marc 'BlackJack' Rintsch




More information about the Python-list mailing list