Confusion: assignments create object references.

Jp Calderone exarkun at intarweb.us
Fri Apr 11 12:41:51 EDT 2003


On Fri, Apr 11, 2003 at 10:28:42AM -0600, Havens, Peter wrote:
> I am confused about when assignments create references to objects as
> opposed to copying the value of those objects.

  That's easy.  It -always- creates references and -never- copies the value
of an object.

  Jp

> I realize this is probably a very common bit of confusion, but I haven't
> had much luck finding anything in the Python FAQ, from googling the
> comp.lang.python news group (voluminous thread tangents), or from the
> regular documentation I've read which has only fueled my confusion. To
> wit, I read the following "rule" in the O'Reilly book "Learning Python":
> 
> "...Python assignment stores references to objects in names or data
> structure slots. It always creates references to objects, instead of
> copying objects."
> 
> That seems fairly straight forward. So, I ran this test:
> 
> >>> L = [0,1,2]
> >>> M = L
> >>> L
> [0, 1, 2]
> >>> M[0] = 9
> >>> L[0]
> 9
> >>>
> 
> So far so good. This works as I would expect given the rule above.
> 
> >>> L = 0
> >>> M = L
> >>> L
> 0
> >>> M = 1
> >>> L
> 1
> >>>
> 

  The above never happened, I assure you. >:)    Python doesn't work this
way.

  Jp

-- 
Examinations are formidable even to the best prepared, for
even the greatest fool may ask more the the wisest man can answer.
                -- C.C. Colton
-- 
 up 22 days, 13:01, 5 users, load average: 0.99, 0.98, 0.99





More information about the Python-list mailing list