Using classes-TypeError: Unbound must be called as body instance...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kevow123
    New Member
    • Sep 2010
    • 4

    Using classes-TypeError: Unbound must be called as body instance...

    TypeError: unbound method draw() must be called with Body instance as first argument (got Turtle instance instead)

    I get that error when I attempt to run my program in python. I have to use the turtle module to draw the body of a car with body class. The class needs to have a init method and a draw method. The init method is supposed to determine fill color and draw is supposed to draw the object.

    heres some of my code:
    Code:
    pen=turtle.Turtle()
    class Body(object):
        def __init__(self,fillcolor=''):
            self.fillcolor=fillcolor
            
        def draw(self,pen,size=80):
            """draw the body of your car of given size using pen size is car length/2; fill it if fillcolor is provided"""
    
            if self.fillcolor:
                pen.fillcolor(self.fillcolor)
                pen.fill(True)
            #draw the Body
    The rest of the code in the draw method is code that draws the body and it works when I separate it from the class in a different program. And it doesn't show but i have imported math and turtle.

    Code:
            pen.down()
            pen.forward(size*3/2)
            pen.left(45)
            pen.forward(size/2*math.sqrt(2))
            pen.left(90)
            pen.forward(size/2*math.sqrt(2))
            pen.left(45)
            pen.forward(size*3/2)
            pen.right(45)
            pen.forward(size/2*math.sqrt(2))
            pen.left(45)
            pen.forward(size*5/4)
            pen.left(45)
            pen.forward(size*1/4*math.sqrt(2))
            pen.left(45)
            pen.forward(size)
            pen.left(45)
            pen.forward(size*1/4*math.sqrt(2))
            pen.left(45)
            pen.forward(size*7/4)
            pen.fill(False)                 # end filling
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You did not show how you called Body(). It should work if you instantiated Body() and called method draw() like this:
    Code:
    obj = Body()
    obj.draw(pen)

    Comment

    • kevow123
      New Member
      • Sep 2010
      • 4

      #3
      thank you very much

      Comment

      Working...