I performed the function to move the points in the rectangle and the values returns none and none for both points. I do not want to return the value when the method in point is preferred, is there another option.
class Point:
def move(self, dx, dy):
'''(Point,number,number)->None
changes the x and y coordinates by dx and dy'''
self.x += dx
self.y += dy
class Rectangle:
def move(self, dx, dy):
'''(Rectangle, number, number) -> None
changes the x and y coordinates by dx and dy'''
self.bottom_left = self.bottom_left.move(dx, dy)
self.top_right = self.top_right.move(dx, dy)
There's no need to assign the result back to the point; Point.move
modifies its argument directly, rather than returning a new Point
object.
class Rectangle:
def move(self, dx, dy):
self.bottom_left.move(dx, dy)
self.top_right.move(dx, dy)
In the rectangle class, if you set the corners with
self.corner = point.move(dx, dy)
the function Point.move() will need to return something, otherwise None is returned by default. You can remedy this by returning self for the Point.move
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def move(self, dx, dy):
'''(Point,number,number)->None
changes the x and y coordinates by dx and dy'''
self.x += dx
self.y += dy
return self
That solves the problem without changing the Rectangle code. You could also do
class Rectangle(object):
def __init__(self, top_right, bottom_left):
self.top_right = Point(*top_right)
self.bottom_left = Point(*bottom_left)
def move(self, dx, dy):
'''(Rectangle, number, number) -> None
changes the x and y coordinates by dx and dy'''
self.bottom_left.move(dx, dy)
self.top_right.move(dx, dy)
which is probably a little better, but the first example explains why you were getting None.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.