Question: Python question. class Point def __init__(self,x,y): self.x=x self.y=y def get_magnitude(self): return math.sqrt(self.x**2+self.y**2) Say we have a Point class as described in the lecture video. We

Python question.

class Point

def __init__(self,x,y):

self.x=x

self.y=y

def get_magnitude(self):

return math.sqrt(self.x**2+self.y**2)

Say we have a Point class as described in the lecture video. We can create Point objects, get their information, and print them nicely.

Now we want to be able to shift an existing Point object by the amount of another Point object.

>>> p = Point(1, 2)

>>> q = Point(3, 4)

>>> #(*** Do a magic incantation to shift p by the amount of q ***)

>>> #What command goes here and how would we implement it?

>>> print(p)

Results in:

Point at (4, 6)

What is the best way to implement this? Please implement the code to do the following.

1.

Create a new point named p consisting of the values of p plus the values of q, so the magic incantation is:

p = Point(p.x + q.x, p.y + q.y)

2.

We don't need to do anything. We get our magic incantation implemented for free:

p = p + q

3.

Create a shift function so the magic incantation is:

shift(p, q)

4.

Implement the method __add__ so the magic incantation is:

p = p + q

Implement the code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!