0

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)

2 Answers 2

2

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.