Searching for a named tuple in a list of named tuples
<p>I need to find out if a given named tuple exists in a list of named tuples (the named tuples are points ex. A(2,3) in a 'Polygon' class). If the given tuple doesn't exist in the list, we append the tuple to the list. If it dos exist, a user defined exception is raised. The function works when the given point doesn't exist on the list. But there's no exception raised if the point does exist and it simply gets added again to the end of the list. And here's what I have so far:
    class ExistingPointError(Exception):
    def <strong>init</strong>(self,value):
        self.value=0</p>

<pre><code>class Polygon(object):
    counter = 0
    def __init__(self):
        Polygon.counter+=1
        self.points = []


# and here's the function that I'm working with

    def setter(self,pt):
        def isThere(pt):
               if pt in self.points: raise ExistingPointError()

            print("Setting Point")
        try:
            isThere(pt)
            self.points.append(pt)
        except ExistingPointError as E:
            print("Point exists! value: ", E)
        print(self.points)


P = Polygon()
point=collections.namedtuple('PointName','Name x y')
A = point(Name = 'A', x = 5, y = 0)
B = point(Name = 'B',x = 10,y = 5)
C = point(Name = 'C',x=5,y=10)
D = point(Name = 'D', x=-2,y=8)
lst = [A,B,C,D]
P.createPolygon(lst)
P.setter(D)
</code></pre>

<p>How about this ?</p>

<pre><code>def setter(self,pt):
    def isThere(pt):
        if pt in self.points:
            raise ExistingPointError()
        print("Setting Point")
    try:
        isThere(pt)
        self.points.append(pt)
    except ExistingPointError as E:
        print("Point exists! value: ", E)
    print(self.points)
</code></pre>

<p>I'm not convinced however that exceptions are the way to go here. Maybe try this :</p>

<pre><code>def setter(self,pt):
    if pt in self.points:
        print("Point exists!")
    else:
        self.points.append(pt)
    print(self.points)
</code></pre>

python list class python-3.x tuples