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>You want to raise a user defined error, ExistingPointError(), yet you haven't really defined what that is. When i run your code and insert a duplicate tuple into a Polygon object, I get the following error:</p>

<pre><code>Traceback (most recent call last):
File "python", line 27, in &lt;module&gt;
File "python", line 20, in setter
NameError: name 'ExistingPointError' is not defined
</code></pre>

<p>You may not need to raise an exception for this as @Gjhuizing mentioned. A simple message telling the user that the object already exists should be enough for your case.</p>

python list class python-3.x tuples