One versus two underscores

19 May, 2013

Omer writes:

I am currently on Chapter 13 and I have come across a problem that I cannot seem to figure out. I am in the section 'Creating the Ball Class'. I wrote the code the way that you have described in the book, and I am getting the following error:

Traceback (most recent call last):
File "C:/Users/Omer/Desktop/Master Functions/PaddleBall.py", line 22, in <module>
ball = Ball(canvas, 'red')
TypeError: object.__new__() takes no parameters

However, when I copied the code from your website, it worked perfectly. I put my code and your code side by side and I cannot seem to figure out where the mistake is. Can you help me figure this out.

It's easy to miss something when you're manually comparing two files. The best way to do this sort of thing is to use a diff tool. You're using Windows, so something like winmerge would do the trick. In any case, when I diff the files you sent using the command line diff tool, I see the following difference:

14c14
<     def _init_(self, canvas, color):
---
>     def __init__(self, canvas, color):

So in your code you've got a single underscore on either side of init - which explains the error you're getting. Because you haven't defined the __init__ function correctly, Python assumes a default with no parameters. Fix that, and the code should work as expected. Good luck!