Page 1 of 1

Python IDLE TypeError Object._new_() takes no parameters

Posted: Sun Mar 03, 2013 4:20 am
by Programmer218
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.

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

Posted: Sun Mar 03, 2013 7:57 am
by KenT
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

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

Posted: Sun Mar 03, 2013 4:35 pm
by Programmer218
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.

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

Posted: Sun Mar 03, 2013 7:27 pm
by KenT
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.

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

Posted: Mon Mar 04, 2013 12:12 pm
by -rst-
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 :(

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

Posted: Mon Mar 04, 2013 10:12 pm
by Programmer218
Yes im sure, I have tried it on all three, 1, 2, and 3. :?:

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

Posted: Tue Mar 05, 2013 11:49 am
by -rst-
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)...