<div class="gmail_quote"><div style="margin-left: 40px;" class="gmail_quote"><div>
People get a confused because if you pass a  mutable object inside a def function and mutate that object the changes /are/ propagated outside-- because now you have a name inside the function and a name outside the object both pointing to the same object.</div>
</div><div><br>Since tuples  are immutable, I guess passing tuples to functions would achieve the result desired by me:<br>result = func ( (anobject) )<br><br><div style="margin-left: 40px;">If you want a function to work on a copy of a mutable object passed to it, and not the real object itself, you must explicitly make a copy of it.<br>
</div><br>Or using deepcopy within the function definition. This would be better as I could simply pass an object to the function. I don't think there is another way to make an object mutable by something in the class definition. Is there?<br>
<br><div style="margin-left: 40px;">>>>class AA:<br>         a=111<br><br>
>>> x=AA()<br>
>>> x.a<br>111<br>
>>> y=x<br>
>>> y.a<br>111<br>
>>> y.a=222<br>
>>> x.a<br>222                         # undesirable?<br>
>>> z=copy.deepcopy(x)<br>
>>> z.a<br>222<br>
>>> z.a=444<br>
>>> x.a<br>
222<br>
>>> y.a<br>
222<br><br></div>
<div style="margin-left: 40px;">The reason you see the "undesirable" behavior of a change to y.a showing the same result of x.a... is because those are the *exact* same instance. You didn't make a copy of the instance, you just made a new name and bound it to the same instance. If you want to copy in Python you have to explicitly do so, via the 'copy' module or any copy methods an object provides.<br>
</div>
<br>Could someone please give me an example of how to implement this copy method for the AA class.<br><br>Going the other way:<br><br>>>> a=555<br>>>> d=copy.copy(a)             # Supposed to be a shallow copy<br>
>>> d<br>555<br>>>> d=444<br>>>> a                                  # But, it doesn't change the original<br>555<br><br>Is there any way to get ints/strings to refer to the same object?<br><br>
Thanks and Regards,<br>Ferdi<br><br></div></div>