By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,691 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

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

P: 4
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:
Expand|Select|Wrap|Line Numbers
  1. pen=turtle.Turtle()
  2. class Body(object):
  3.     def __init__(self,fillcolor=''):
  4.         self.fillcolor=fillcolor
  5.  
  6.     def draw(self,pen,size=80):
  7.         """draw the body of your car of given size using pen size is car length/2; fill it if fillcolor is provided"""
  8.  
  9.         if self.fillcolor:
  10.             pen.fillcolor(self.fillcolor)
  11.             pen.fill(True)
  12.         #draw the Body
  13.  
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.

Expand|Select|Wrap|Line Numbers
  1.         pen.down()
  2.         pen.forward(size*3/2)
  3.         pen.left(45)
  4.         pen.forward(size/2*math.sqrt(2))
  5.         pen.left(90)
  6.         pen.forward(size/2*math.sqrt(2))
  7.         pen.left(45)
  8.         pen.forward(size*3/2)
  9.         pen.right(45)
  10.         pen.forward(size/2*math.sqrt(2))
  11.         pen.left(45)
  12.         pen.forward(size*5/4)
  13.         pen.left(45)
  14.         pen.forward(size*1/4*math.sqrt(2))
  15.         pen.left(45)
  16.         pen.forward(size)
  17.         pen.left(45)
  18.         pen.forward(size*1/4*math.sqrt(2))
  19.         pen.left(45)
  20.         pen.forward(size*7/4)
  21.         pen.fill(False)                 # end filling
  22.  
Nov 26 '10 #1

✓ answered by bvdet

You did not show how you called Body(). It should work if you instantiated Body() and called method draw() like this:
Expand|Select|Wrap|Line Numbers
  1. obj = Body()
  2. obj.draw(pen)

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You did not show how you called Body(). It should work if you instantiated Body() and called method draw() like this:
Expand|Select|Wrap|Line Numbers
  1. obj = Body()
  2. obj.draw(pen)
Nov 26 '10 #2

P: 4
thank you very much
Nov 26 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.