Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Helping understand classes
#1
Question 
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
Reply
#2
You have to step through the code step-by-step
with the help of a debugger.
Reply
#3
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.
Reply
#4
This is the outout:

Output:
Rectangle with area 6.0 Rectangle with area 6.0 Circle with diameter 2.0
Reply
#5
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 ?
99 percent of computer problems exists between chair and keyboard.
Reply
#6
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:

Output:
Rectangle with area 6.0 Rectangle with area 6.0 Circle with diameter 2.0

Could you explain how thats the output? I understand what the code is doing until the reorder function
Reply
#7
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.
Reply
#8
(Nov-27-2017, 01:43 AM)Larz60+ Wrote: 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.

Thank you! I used http://pythontutor.com/visualize.html#mode=display and it helped me understand what each line is doing.
Reply
#9
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
99 percent of computer problems exists between chair and keyboard.
Reply
#10
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dijkstra algorithm helping find new space 1bumcheek 7 979 Jul-28-2023, 08:30 AM
Last Post: Gribouillis
  helping PyInstaller To Find files Harshil 0 1,489 Aug-30-2020, 10:16 AM
Last Post: Harshil
  Trying to understand classes menator01 7 3,293 Oct-27-2019, 04:26 PM
Last Post: menator01
  Help me understand this... (Classes and self). Ceegen 15 8,257 Mar-31-2019, 10:41 PM
Last Post: Yoriz
  Using classes? Can I just use classes to structure code? muteboy 5 5,066 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020