Using Instances From Other Classes, Overriding And Separating Values Of X And Y From A Point(x,y) From One Class To Another
Solution 1:
Your first problem comes when re-initialising the points in your Figure class's init method:
Point.__init__(bottom_left)
Point.__init__(top_right)
If you check the 'Point' class's init method, it takes in the parameters 'self, xcoord=0, ycoord=0'. So what you are doing is sending in the 'bottom_left' and 'top_right' points as the 'self' parameter, with default values for the x- and y-coordinates. This sets these points' values to 0.
There is no need to call these initialisations; you have already initialised the points when you called Point() and Point(1,2).
To address your concerns:
1) You are working with a Figure object, not a Point object in the Figure repr and str methods. 'Figure' does not have 'x' and 'y' attributes, just 'bottom_left' and 'top_right' (both objects of type Point). So reference these instead.
1a) Addressed above: you do not need to re-initialise the Point objects. You just need to set 'bottom_left' to the entered 'bottom_left' object and the same for 'top_right' (which you are doing).
2) By creating your own repr and str methods in 'Figure', you are overriding the default 'object' class methods, not the 'Point' methods. You can leverage off Point's implementations instead of repeating everything though. eg:
def__str__(self):
returnstr(self.bottom_left) + ', ' + str(self.top_right)
3) Examine the method parameters of setx(). What does it expect? Hint: not a Point object!
Solution 2:
Firstly, you have a few syntax errors in your code:
def__init__(self, xcoord=0, ycoord=0)
is missing a :
at the end.
return'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'return'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'
have missing or extra '
in them.
1) Yes, those methods change how the class instance is created and represented. __str__
and __repr__
are failing because when they're called (when you try to print
that instance) the code under those two methods is failing. Looks like you want to return the value of the point's coordinates, more like this:
def__repr__(self):
return'Point(' + str(self.bottom_left.x) + ',' + str(self.bottom_left.y) + '), ' + 'Point(' + str(self.top_right.x) + ',' + str(self.top_right.y) + ')'def__str__(self):
return'Point(' + str(self.bottom_left.x) + ',' + str(self.bottom_left.y) + '), ' + 'Point(' + str(self.top_right.x) + ',' + str(self.top_right.y) + ')'
a) You don't need to explicitly call Point.__init__
in your Figure
__init__
method; that happens already when you pass it Point()
.
2) Not quite sure what you mean here, not sure how else you'd do it without overriding these methods.
3) Here you are not printing Point()
after it's had setx
called; you're printing the return of setx
. None
is what is returned from any Python function that doesn't explicitly return something. Your setx
function doesn't have a return
, so it returns None
. It still works, in that it still sets x. Try:
p = Point()
p.setx(44)
print(p)
Post a Comment for "Using Instances From Other Classes, Overriding And Separating Values Of X And Y From A Point(x,y) From One Class To Another"