method returns object reference

Ned Batchelder ned at nedbatchelder.com
Tue Jun 23 16:59:03 EDT 2015


On Tuesday, June 23, 2015 at 2:32:55 PM UTC-4, Tim wrote:
> I spent a while finding this problem which looks something like the old "mutable default argument" problem. http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments
> 
> I'm not crystal clear on the details, but once I found it, the fix was easy.
> I'm posting here just because I thought it was interesting.
> 
> The situation is that I have a Worker() which has a Client(). 
> The worker queries the client; client returns its self.response
> worker manipulates that response. Now client's response has same changes.

Assignment in Python never makes a copy, it only makes a new name refer to
an existing value.  This talk covers the details in depth:

    http://nedbatchelder.com/text/names1.html

--Ned.



More information about the Python-list mailing list