function scope

Baris Demir demirbaris at gmail.com
Mon Feb 2 18:40:22 EST 2009


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

cheers

B.D.
   
   
   



More information about the Python-list mailing list