checking if an object IS in a list

Peter Otten __peter__ at web.de
Fri Jul 18 18:19:51 CEST 2008


nicolas.pourcelot at gmail.com wrote:

>> 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. ;-)

I won't pretend I understand ;)

If you make Point immutable you might be able to drop the "must not be
referenced twice" requirement.

Peter



More information about the Python-list mailing list