unexpected behavior: did i create a pointer?

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Fri Sep 7 05:41:13 EDT 2007


gu schreef:
> copyOfA = a
> 
> now, in the second "for" cycle and in functionA() i only 'touch' copyOfA 
> (altering it).

copyOfA isn't a copy of a; it's a different name bound to the same 
object as a. You can verify that: id(a) and id(copyOfA) will return the 
same value.

To make a copy of a (assuming a is a dict), you can do:

copyOfA = dict(a)

or

copyOfA = a.copy()

or more generally

import copy
copyOfA = copy.copy(a)


Cheers,
Roel

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven



More information about the Python-list mailing list