Helping understand classes - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Helping understand classes (/thread-6504.html) Pages:
1
2
|
Helping understand classes - Miraclefruit - Nov-26-2017 According to the solutions, the following code prints out Square with side 5.0 Rectangle with area 6.0 Circle with radius 2.0 Square with side 6.0 Rectangle with area 6.0 Circle with radius 1.0 class Shape(object): def __eq__(s1, s2): return s1.area() == s2.area() def __lt__(s1, s2): return s1.circum() < s2.circum() class Rectangle(Shape): def __init__(self, h, w): self.height = float(h) self.width = float(w) def circum(self): return 2*(self.height + self.width) def __str__(self): return 'Rectangle with area ' + str(self.height*self.width) class Square(Rectangle): def __init__(self, s): Rectangle.__init__(self, s, s) def __str__(self): return 'Square with side ' + str(self.height) class Circle(Shape): def __init__(self, radius): self.radius = float(radius) def circum(self): return 3.14159*(2*self.radius) def __lt__(self, other): return self.radius < other.radius def __str__(self): return 'Circle with diameter ' + str(2.0*self.radius) def reorder(L): for e in L: if e < L[0]: L[0] = e L = [Square(6), Rectangle(2, 3), Circle(1)] try: reorder(L) for e in L: print(e) except: for e in L: print(e )Could someone care to explain how that's the solution? Thank you RE: Helping understand classes - heiner55 - Nov-26-2017 You have to step through the code step-by-step with the help of a debugger. RE: Helping understand classes - buran - Nov-26-2017 There is no way this code could produce that output - the L list has only 3 objects in it if not for anything else. There are number of other problems, incl. with the way try/except is used. RE: Helping understand classes - heiner55 - Nov-26-2017 This is the outout:
RE: Helping understand classes - Windspar - Nov-26-2017 1. Put some line space in code. My eyes hurt from reading that. class Shape(object): def __eq__(s1, s2): # == return s1.area() == s2.area() def __lt__(s1, s2): # < return s1.circum() < s2.circum() class Rectangle(Shape): def __init__(self, h, w): self.height = float(h) self.width = float(w) def circum(self): return 2*(self.height + self.width) def __str__(self): # use for print return 'Rectangle with area ' + str(self.height*self.width) class Square(Rectangle): def __init__(self, s): Rectangle.__init__(self, s, s) def __str__(self): return 'Square with side ' + str(self.height) class Circle(Shape): def __init__(self, radius): self.radius = float(radius) def circum(self): return 3.14159*(2*self.radius) def __lt__(self, other): return self.radius < other.radius def __str__(self): return 'Circle with diameter ' + str(2.0*self.radius) def reorder(L): for e in L: if e < L[0]: # only compares to first value L[0] = e # only changes first value if __name__ == '__main__': L = [Square(6), Rectangle(2, 3), Circle(1)] try: reorder(L) for e in L: print(e) except: for e in L: print(e )2. Would need to know example to be sure. I would say you mess up reorder function. L[0] = e # only replace first item Another thing I notice why does Shape compare area. When no object have an area method ? RE: Helping understand classes - Miraclefruit - Nov-27-2017 This is one of the quiz solutions from the MIT courseware. So I'm really confused as to how they got that. (Nov-26-2017, 10:58 AM)heiner55 Wrote: This is the outout: Could you explain how thats the output? I understand what the code is doing until the reorder function RE: Helping understand classes - Larz60+ - Nov-27-2017 To really understand how this code works, load it up with a debugger and single step through the entire process, looking at all the variables along the way. I've done a lot of reverse engineering of code, often at the machine code level. In which case, I would single step through a routine, checking all registers, memory, etc. Debuggers help you to do the same with higher level languages. I don't knoe if you use an IDE or not, but most come with built in debuggers. If not, do a google search on "Python IDE's" and find one that does. Personally, I like PyCharm, but that's strictly a personal thing. There are many good IDE's out there. RE: Helping understand classes - Miraclefruit - Nov-27-2017 (Nov-27-2017, 01:43 AM)Larz60+ Wrote: To really understand how this code works, load it up with a debugger and Thank you! I used http://pythontutor.com/visualize.html#mode=display and it helped me understand what each line is doing. RE: Helping understand classes - Windspar - Nov-27-2017 Really Mit quiz courseware. I thought they be harder. Just walk through the code. def reorder(L): for e in L: #e will be Square then Rectangle, and finally a Circle if e < L[0]: # e always compares to Square. You are compare __lt__ overload. # round 1 Square < Square = False # round 2 Rectangle < Square = True # round 3 Circle < Square = False L[0] = e # Square get Replace by Rectangle RE: Helping understand classes - heiner55 - Nov-27-2017 You can try IDE "spyder3" if you are on a Linux system, because on my system it was already installed. (Sypder3 is a simple IDE) |