473,385 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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:
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)

2 2613
bvdet
2,851 Expert Mod 2GB
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
thank you very much
Nov 26 '10 #3

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

Similar topics

12
by: bissatch | last post by:
Hi, Generally if I re-use code, I use a function. If I need to use these functions over a number of pages I write the function to an include file where all pages have access. So when should I...
6
by: Jake D | last post by:
I understand how to load a DLL in C++ and use functions from it, however I have a DLL which I did not create which has some classes in it. I can't seem to find out how to use the classes to make...
1
by: Donald Welker | last post by:
I'm building a web forms project and would like to provide consistent connection, data adapter, dataset objects, etc. across multiple ASPX pages. I'm having problems with trying to make these...
0
by: Stu | last post by:
Hi, Ive been using classes as output from webservices which until now have worked well and have serialised into the xml i wanted. Now i want to have a structure of clients which have accounts...
0
by: The Confessor | last post by:
I have a rather sprawling application-in-progress in Visual Basic .NET 2003, and I'm using very specific naming conventions to avoid confusion and make the function of my code as self-evident as...
5
by: Richard Gromstein | last post by:
Hello, I have an exercise that I need to finish very soon and I really need help understanding what to do and how exactly to do it. I am working on reading the chapter right now and working on it...
3
by: apedosmil8 | last post by:
I am having problems with using classes. I'm not sure why this program will not run. I have pointed out the problem. Thanks #include <iostream> #include <iomanip> #include <cmath> using...
2
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello 1) I agree that useing classes is the best way to use VB.net. The question is how do you write a VB program without using classes? 2) For my own edification where can I see an actual...
3
by: Ramu528 | last post by:
Hi friends, I am populating a drop box from a table jos_qualifications. can anybody tell me how to populate a drop box using classes. something like $db->setQuery( $query ); this is my code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.