checking if an object IS in a list

nicolas.pourcelot at gmail.com nicolas.pourcelot at gmail.com
Fri Jul 18 10:39:38 EDT 2008


> What is your (concrete) use case, by the way?



I try to make it simple (there is almost 25000 lines of code...)
I have a sheet with geometrical objects (points, lines, polygons,
etc.)
The sheet have an object manager.

So, to simplify :

>>> sheet.objects.A = Point(0, 0)
>>> sheet.objects.B = Point(0, 2)
>>> sheet.objects.C = Middle(A, B)

Then we have :

>>> sheet.objects.A == sheet.objects.B
True

since have and B have the same coordinates.
But of course A and B objects are not same python objects.
In certain cases, some geometrical objects are automatically
referenced in the sheet, without being defined by the user.
(Edges for polygons, for example...)
But they must not be referenced twice. So if the edge of the polygon
is already referenced (because the polygon uses an already referenced
object for its construction...), it must not be referenced again.
However, if there is an object, which accidentally have the same
coordinates, it must be referenced with a different name.

So, I use something like this in 'sheet.objects.__setattr__(self,
name, value)':
if type(value) == Polygon:
    for edge in value.edges:
        if edge is_in sheet.objects.__dict__.itervalues():
            object.__setattr__(self, self.__new_name(), edge)

Ok, I suppose it's confused, but it's difficult to sum up. ;-)

> Another possibility :-)
> from itertools import imap
> id(x) in imap(id, items)

I didn't know itertools.
Thanks :-)



More information about the Python-list mailing list