I have a class Point
. Its __init__
method checks if there is a point on turtle canvas. If it finds that it is there already, I need to prevent the init from any further execution.
points = []
class Point():
def __init__(self, pen, points=points):
self.x = pen.xcor()
self.y = pen.ycor()
for point in points:
if point == (self.x, self.y, pen):
# here, I need to stop __init__ from executing
return True# this raises an error, but does not stop
#the function the way I want
points.append((self.x, self.y, pen))
self.pen = pen
self.get()
point1 = Point(p)
Here's what I get:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
point1 = Point(p)
TypeError: __init__() should return None, not 'bool'
As the error message states, __init__()
should return None
, not 'bool
'. Change return True
to a bare return
(which is equivalent to return None
).
for point in points:
if point == (self.x, self.y, pen):
return
Its
__init__
method checks if there is a point on turtle canvas. If it finds that it is there already, I need to prevent the init from any further execution.
That said, the difficulty you're having is indicative of a design flaw. A point shouldn't check an external list of points and refuse to be constructed; rather, the code that's creating the point should do the check.
I would hoist the loop out of the constructor and have the caller do the check.