Note that there are some explanatory texts on larger screens.

plurals
  1. POInitializing superclasses in Python
    primarykey
    data
    text
    <p>I'm learning Python. At the moment, I can do what I want to do by composition, but when I try to do the same thing using inheritance, I get an error. Here's my code. I'm basically trying to make a class for a colored square. </p> <pre><code>from graphics import * class Block(Rectangle): def __init__(self, corner, colour): self.corner = corner self.colour = colour self.x1 = self.corner.getX() * 30 self.y1 = self.corner.getY() * 30 self.x2 = self.x1 + 30 self.y2 = self.y1 + 30 self.point1 = Point(self.x1, self.y1) self.point2 = Point(self.x2, self. y2) Rectangle.__init__(self, self.point1, self.point2) def draw(self, window): self.window = window self.Rectangle.draw(self.window) new_win = GraphWin("thingy", 700, 500) corner = Point(1, 1) square1 = Block(corner, 'red') square1.draw(new_win) new_win.mainloop() </code></pre> <p>The error I get is</p> <pre><code>File "F:\Python\4\4_3.py", line 24, in draw self.draw(self.window) </code></pre> <p>The error is repeated indefinitely.</p> <p>Here is the code that does what I want when I do it with composition:</p> <pre><code>from graphics import * class Block(): def __init__(self, corner, colour): self.corner = corner self.colour = colour self.x1 = self.corner.getX() * 30 self.y1 = self.corner.getY() * 30 self.x2 = self.x1 + 30 self.y2 = self.y1 + 30 self.point1 = Point(self.x1, self.y1) self.point2 = Point(self.x2, self. y2) self.Rectangle = Rectangle(self.point1, self.point2) def draw(self, window): self.window = window self.Rectangle.draw(self.window) self.Rectangle.setFill(self.colour) new_win = GraphWin("thingy", 150, 150) corner = Point(1, 1) square1 = Block(corner, 'red') square1.draw(new_win) new_win.mainloop() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload