Programmer218
Posts: 3
Joined: Sun Mar 03, 2013 4:02 am

Python IDLE TypeError Object._new_() takes no parameters

Sun Mar 03, 2013 4:20 am

Hi,
I have written a simple paddleball game with python but then this error happened.
TypeError: Object._new_() takes no parameters
this is the part i believe is wrong,
class Ball:
def _init_(self, canvas, color):
self.canvas = canvas

please help.

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: Python IDLE TypeError Object._new_() takes no parameters

Sun Mar 03, 2013 7:57 am

init has two underlines on each side.

Also def __init__ should be indented, which you might have done. Use code blocks in your post and indents will be retained

Code: Select all

class Ball:
    def __init__(self, canvas, color):
         self.canvas = canvas
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

Programmer218
Posts: 3
Joined: Sun Mar 03, 2013 4:02 am

Re: Python IDLE TypeError Object._new_() takes no parameters

Sun Mar 03, 2013 4:35 pm

I do have def indented and stuff srry about that, but i dont think thats wrong now i believe it might be,
tk.update

ball = Ball(canvas, 'red')

the error says
Traceback (most recent call last):
File (my file's name), line 38, in <module>
ball = Ball(canvas, 'red')
TypeError: object._new_() takes no parameters

I dont know which one is wrong or if they both are.

KenT
Posts: 758
Joined: Tue Jan 24, 2012 9:30 am
Location: Hertfordshire, UK
Contact: Website

Re: Python IDLE TypeError Object._new_() takes no parameters

Sun Mar 03, 2013 7:27 pm

The error suggests that Python cannot find the __init__ function in Ball so its reverting to _new_ which takes no arguments.

Which python 2.7 or 3 I'm assuming 2.7 and do not know if 3 is different.
Pi Presents - A toolkit to produce multi-media interactive display applications for museums, visitor centres, and more
Download from http://pipresents.wordpress.com

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Python IDLE TypeError Object._new_() takes no parameters

Mon Mar 04, 2013 12:12 pm

Programmer218 wrote:I do have def indented and stuff srry about that, but i dont think thats wrong now i believe it might be,
...
I dont know which one is wrong or if they both are.
Are you absolutely sure you have two underlines/underscores both sides of the 'init' in the declaration (def)??

This is what I get in my Python 2.7 on Windows:

Code: Select all

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> class Ball:
    def __init__(self, canvas, color):
         self.canvas = canvas
>>> canvas = 1
>>> ball = Ball(canvas, 'red')
>>> 
...so works fine (note that I just used a dummy value for canvas to keep it simple)!

However, if I use only one underline/underscore:

Code: Select all

>>> class Ball:
	def _init_(self, canvas, color):
		self.canvas = canvas

		
>>> ball = Ball(canvas, 'red')

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    ball = Ball(canvas, 'red')
TypeError: this constructor takes no arguments
>>> 
Personally I think the requirement for double underscore is the most annoying thing I have come against in Python so far - utterly confusing as most typefaces do not separate the underscores, so hard to realise when looking at example code :(
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Programmer218
Posts: 3
Joined: Sun Mar 03, 2013 4:02 am

Re: Python IDLE TypeError Object._new_() takes no parameters

Mon Mar 04, 2013 10:12 pm

Yes im sure, I have tried it on all three, 1, 2, and 3. :?:

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Python IDLE TypeError Object._new_() takes no parameters

Tue Mar 05, 2013 11:49 am

Any chance you could post the full code?

Within the 'code' tag please (see the button labelled 'Code' above the textarea when writing a post here)...
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “General programming discussion”