【发布时间】:2016-10-10 17:39:40
【问题描述】:
我需要找出给定的命名元组是否存在于命名元组的列表中(命名元组是点,例如“多边形”类中的 A(2,3))。如果列表中不存在给定的元组,我们将元组附加到列表中。如果确实存在,则会引发用户定义的异常。当列表中不存在给定点时,该函数起作用。但是,如果该点确实存在并且它只是再次添加到列表的末尾,则不会引发异常。这是我到目前为止所拥有的: 类 ExistingPointError(异常): def init(自我,价值): self.value=0
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)
【问题讨论】:
-
您应该发布
pt的样子,即您如何定义命名元组。 -
欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。具体来说,您还没有发布重现问题的代码。
-
isThere中的 for 循环没有任何意义。
标签: python list class python-3.x tuples