Re: scared about refrences...



I think you are afraid of references because you are not trusting your
own code. You are afraid it will do "magic" behind the scenes and mess
everything up. One way around that is to simply write better code and
test often.

If everything was copied when passed around it would be pretty awful --
imagine passing around a 1Gb worth of data to a function...

In Python all the primitives are copied and all other entities are
references. If you want to just copy a list you can :
1) use the list class: new_list=list(old_list)
2) use the [:] syntax: new_list=old_list[:]
3) if your list has nested lists, for example, the above methods will
not copy everything, so you need a deep copy -- copy.deepcopy()

Hope this helps,
Nick V.


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....

.



Relevant Pages

  • Re: A list of archaeological connections would be nice
    ... They give references. ... big-word stories are lists and lists lists of names and dates ... give proper scholarly references. ... Yes, it's not my fault, and no, ...
    (sci.archaeology)
  • Re: Error 3464 with dCount - some computers only
    ... a minor correction to my original post. ... The SQL for the query which is producing the error in the dcount statement ... restarted and re-installed all references ... Reference lists on both computers again identical. ...
    (microsoft.public.access.formscoding)
  • Re: why cannot assign to function call
    ... it's not the assignment model which is strange at all. ... I want *Python* semantics, and amazingly enough, that's ... there are no references and no dereferencing. ... lists and tuples store *objects*. ...
    (comp.lang.python)
  • Re: An oddity in list comparison and element assignment
    ... I'm not convinced that equality shouldn't imply invariance ... for each corresponding element in the two lists, ... have other references bound to them. ... equality test has to claim that l1 and l3 are not equal, ...
    (comp.lang.python)
  • Re: GC performance - GC fragility
    ... it the option to permanently let the developer to think about how ... Now remove the object pointer from one of these lists. ... in question is doing by keeping those 10 references in those 10 lists. ... I don't really see that this is a question of GC vs non-GC, ...
    (borland.public.delphi.non-technical)