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().
Is this ever supposed to happen? I just thought this was odd. This happens
when I am trying to copy a list of objects (sheets ) in the current object
into a object which is also in a list( not the same one). Then I try to copy
a object (shape) from a global list into a list inside a sheet object which
is inside a list in the node object.
The real problem I am having is when from in a method of an object(node), I
copy a self.list of objects ( using copy.copy ) into a new node that I have
created. This bit seems to work all right. The problem happens when I
insert a object(shape) into a list within a node objects list of shapes. I
hope this all makes sense...
What it does is make the shape appear in sheet object in current node as
well. This shouldn't happen.
Any tips for getting around referencing problems? Like what are the prefered
ways to copy a list? I didn't post the source cause I thought it was a bit
big.
Thanks for any help in advance.
Sent via Deja.com http://www.deja.com/
Before you buy.
> 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().
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
and unless malloc or something has gone wrong then id() will always return
unique numbers for different objects[0].
So if you've genuinely got a list with something inside the list with the
same id, you've probably append'ed your list into itself.
Laurie
[0] With the - obvious, given the implementation - caveat that objects with
different lifetimes may have the same id
--
http://eh.org/~laurie/comp/python/
Forgot to mention, I'm using python 1.5.2 on linux 2.2.12, on a cyrix 266,
and a celron system, same problems on both. I don't know about the lifetime
problem, but I'm not appending the list into itself, the object in there is a
instance of a shape object. Could the problem be with copy.copy maybe? The
library reference docs don't sound to reassuring. I might try moving the
method out of the object into a function. But that will take a while, and
should use up heaps more memory :(
Thanks for your reply.
>>> 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
> 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/
It is odd.
Here's how it could happen:
>>> a = []
>>> a.append(a)
>>> id(a)
135070976
>>> id(a[0])
135070976
Let's try printing this:
>>> a
[[...]]
The list has a pointer to itself. I'm not sure if this is the same
problem you're seeing, but you might poke around to see if you're
accidentally using some list instead of some list[some index].
- Amit
Looks like I was doing something wrong for the id. After a nights sleep I
found it in the morning. Thanks for the help Laurence and Amit :)
It seems that deepcopy doesn't work for what I am doing. That is tempNode =
copy.deepcopy(self) because tempNode.value[0].listOfShapes and
self.value[0].listOfShapes have the same id. I guess this is reasonable. So
I just manually copy the list with a new one, not too hard now that I know :)
If the id()'s are the same, then you have managed to create a circular
reference in your list. Eg:
Python 1.5.2 (#8, May 12 1999, 17:46:31) [GCC 2.7.2.1] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> x = []
>>> x.append(x) # create a circular reference
>>> id(x)
135060792
>>> id(x[0])
135060792
Double-check your code to make sure that the list assignments can
never reference themselves (unless that's what it's supposed to do, of
course). Lots of can't-happen cases turn out to actually be possible
when the code is run. :(
Note that circular references can't be automatically garbage-collected
under CPython -- only Viper and JPython can currently gc circular
references. This isn't normally a problem unless you create a *lot* of
circular garbage, or if the script will run for a long time (like as a
daemon process).
Neel