[Tutor] assignment statements in python

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Jun 12 16:20:19 CEST 2006


>> Assignment in Python is not a copy, it is a name binding. Assignment 
>> creates a name for an object. If you assign the same object to two 
>> names, they both are bound to the same thing. If the object is mutable, 
>> like a list, changes to the object will be seen regardless of which 
>> name you use to refer to it.
>>
>> ******
>
> In that case, is it possible to copy a variable by value, instead of by 
> reference, in Python?

The 'copy' module is available,

     http://www.python.org/doc/lib/module-copy.html

So in a pinch, if we're really paranoid, we can pass copies of our 
argument values to a function.  In general, though, a function should 
really document if it mutates its arguments, because that's generally a 
very rude thing to do unless it's the expected behavior.



More information about the Tutor mailing list