obj in list and list ids the same

Laurence Tratt tratt at dcs.kcl.ac.uk
Tue Dec 28 15:08:12 EST 1999


In message <84ar5o$6n4$1 at nnrp1.deja.com>
          rdudfield at my-deja.com wrote:

[rdudfield at my-deja.com]
>>> I've got a problem where a list has the same id as a object instance in
>>> that same list.  This is as returned by id().
[Laurie]
>> If you're using CPython, then this is *very* unlikely (I can't speak for
>> the other implementations); id() returns the memory address of a given
>> object
[rdudfield at my-deja.com]
> Could the problem be with copy.copy maybe?

I doubt it, but it's worth knowing that copy.copy on a list is basically
equivalent to list[:], so:

  a = [ ... ]
  a_copy1 = copy.copy(a)
  a_copy2 = a[:]

will both return new *shallow* copies of a. If you want to copy the list,
and also make a brand new copy of everything inside it, you need
copy.deepcopy.

Somewhere I presume you have code like:

  l = [ ... ]
  print id(l)
  for item in l:
    if id(item) == id(l):
       print "List '%s' and item '%s' have same ids" % (str(l), str(item))
       print item is l        # See what 'is' has to say about the objects;
                              #   this won't work if 'is' is implemented
                              #   using id() or equivalent

I think you need to double check to see you really are getting different
objects yielding the same id() at the same time. Check what C extension
modules you are using; perhaps one of them is getting its ref counts in a
twist. As far as I know, Python 1.5.2 doesn't have any known leaks; but less
well known extensions may have leaks.


Laurie
-- 
http://eh.org/~laurie/comp/python/



More information about the Python-list mailing list