Baris Demir wrote: > Hi everybody, > > I am quite new to python and using it for my thesis. Luckily I found > out some kind of behavior surprising to me and so unwanted in my code. I > could not find any explanation, so solution for the code. > It is simply like this: > > /*li = another_module.global_variable > f=simpleCut(li) > > def simpleCut(d=dict()): > temp=d > for i in temp.keys(): > if (temp[i] == .......) : > temp[i]=new_value > return temp > */ > here /*simpleCut(d=dict())*/ is a function that changes the value of the > key in the d if it satisfies conditions and returns the new dictionary. > what i do not understand is, when i checked the original dictionary > /*li,*/ i also found out that, the value of that key(operated in the if > statement) had also been changed. so, simply, the simpleCut function > also changes the variables of its own argument, even there is a 'temp' > variable. That is a scandal for my code:) > > please tell me, if this is not possible and i am missing another thing > in my code, or if this kind of behavior is possible, what is the > philosophy of it, and how can i change it??? > It's called "Reference Semantics".
"d" refers to a dict. When you say "temp=d" it makes "temp" also refer to that dict; it doesn't copy the dict. If you want a copy when you have to do so explicitly with "temp=d.copy()". When you pass "li" into simpleCut() you're not making a copy either! -- http://mail.python.org/mailman/listinfo/python-list