First of all thank you for taking your time to consider the following inquiry. English is not my main language so I tried to be as clear as I could so I apologize in advance for it.
Considering this code for the questions (keeping in mind that I cannot change any of the code in the class Point):
class Point:
def __init__(self, xcoord=0, ycoord=0)
self.x = xcoord
self.y = ycoord
def setx(self, xcoord):
self.x = xcoord
def sety(self, ycoord):
self.y = ycoord
def __repr__(self):
return 'Point('+str(self.x)+','+str(self.y)+')'
def __str__(self):
return 'Point('+str(self.x)+','+str(self.y)+')'
class Figure:
def __init__(self, bottom_left, top_right):
Point.__init__(bottom_left)
Point.__init__(top_right)
self.bottom_left = bottom_left
self.top_right = top_right
def get_bottom_left(self):
print(Point().setx(self.bottom_left))
print (self.bottom_left)
def get_top_right(self):
print(self.top_right)
def __repr__(self):
return 'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'
def __str__(self):
return 'Point('+str(self.x)+','+str(self.y)+') + ', Point('+str(self.x)+','+str(self.y)+')'
When assigning
f = Figure(Point(),Point(1,2))
1) To my knowledge, init, repr and str are python methods that by being in a class they modify the object without being called explicitly. So the reason repr and str are in both classes is to be able to get the output "Point(0,0), Point()" when inputing f in the interpreter. But when I try that I get that Figure has no attribute 'x'.
a) I manage to create the x and y values when using a single point directly into the class Point, but I can't seem to do that when doing it through class Figure even though I am initializing bottom_left and top_right in the class Figure, through the class Point by using
Point.init(bottom_left)
In other words, I don't know how to break down Figure(Point(),Point(1,2)) into:
Point() = x = 0, y = 0
Point(1,2) = x = 1, y = 2
Through the use of the first class
2) I don't know how to "override a Python's function" without using Inheritance, is it even possible? By this I am referring to the methods I am using here: init, repr and str.
3) When trying to access function setx() from class Point through the
print(Point().setx(self.bottom_left))
I get the value none. When I put a return (just to test because I can't do this) in setx() it returns Point(0,0) instead of just the x value.
My guess is that I have to use inheritance, but I was suggested to just use instances from class Point.
I usually don't ask this often after another previous post, but I am running out of time at this point and I really want to understand the core of the problem (This is a more simplified version of the actual assignment for me to be able to attack my core misunderstandings which is in general interconnecting values, objects and instances from one class to another).