Newbie question: Strange TypeError

T

Tim Isakson

Howdy,

I'm a new python programmer - new to python, not so much to programming -
my background was mainly C, with some C++, but it's rusty.

I'm learning python to try and get back into some coding,
and am running into a problem - I'm writing a calculator in python and
wxPython as an exercise to learn both, and the following problem is
leaving me baffled.

I have an event handler, and it's getting called fine - but within the
event handler, I'm calling a class method, HandleInput. The definition
and call are shown below:

def HandleInput(self, input):
if self.results.GetValue() == "0":
return input
elif newVal == True:
return input
else:
tmpVal = self.results.GetValue() + input
return tmpVal

def OnButton1(self,e):
tmpVal = self.HandleInput(self, "1")
self.results.SetValue(tmpVal)

The call to HandleInput is made, but I get the following error:

Calling HandleInput(self, '1')
Traceback (most recent call last):
File "pycalc.py", line 115, in OnButton1
tmpVal = self.HandleInput(self, "1")
TypeError: HandleInput() takes exactly 2 arguments (3 given)

So far as I can tell, the offending call *IS* calling HandleInput with 2
arguments, but the interpreter obviously thinks different, and I'm at a
loss as to why that might be.

I'm sure I'm doing something odd and not noticing, and I'd appreciate any
help you can provide!

Thanks in advance,

Tim
 
B

Bruce Wolk

Tim said:
Howdy,

I'm a new python programmer - new to python, not so much to programming -
my background was mainly C, with some C++, but it's rusty.

I'm learning python to try and get back into some coding,
and am running into a problem - I'm writing a calculator in python and
wxPython as an exercise to learn both, and the following problem is
leaving me baffled.

I have an event handler, and it's getting called fine - but within the
event handler, I'm calling a class method, HandleInput. The definition
and call are shown below:

def HandleInput(self, input):
if self.results.GetValue() == "0":
return input
elif newVal == True:
return input
else:
tmpVal = self.results.GetValue() + input
return tmpVal

def OnButton1(self,e):
tmpVal = self.HandleInput(self, "1")
self.results.SetValue(tmpVal)

The call to HandleInput is made, but I get the following error:

Calling HandleInput(self, '1')
Traceback (most recent call last):
File "pycalc.py", line 115, in OnButton1
tmpVal = self.HandleInput(self, "1")
TypeError: HandleInput() takes exactly 2 arguments (3 given)

So far as I can tell, the offending call *IS* calling HandleInput with 2
arguments, but the interpreter obviously thinks different, and I'm at a
loss as to why that might be.

I'm sure I'm doing something odd and not noticing, and I'd appreciate any
help you can provide!

Thanks in advance,

Tim

The error message gave you what you needed. Bound methods, when called,
do not supply an argument for the first formal parameter ("self" by
convention). Since HandleInput was defined using two formal parameters,
it must called using only one. So change the offending statement to

tmpVal = self.HandleInput("1")
 
A

Adrien Di Mascio

Howdy, Hi,


def HandleInput(self, input):
if self.results.GetValue() == "0":
return input
elif newVal == True:
return input
else:
tmpVal = self.results.GetValue() + input
return tmpVal

def OnButton1(self,e):
tmpVal = self.HandleInput(self, "1")
self.results.SetValue(tmpVal)
The call to HandleInput is made, but I get the following error:

Calling HandleInput(self, '1')
Traceback (most recent call last):
File "pycalc.py", line 115, in OnButton1
tmpVal = self.HandleInput(self, "1")
TypeError: HandleInput() takes exactly 2 arguments (3 given)
So far as I can tell, the offending call *IS* calling HandleInput with 2
arguments, but the interpreter obviously thinks different, and I'm at a
loss as to why that might be.
Well, your script *IS* calling HanlderInput with 3 arguments since
there is the 'hidden' object instance as first argument. You must not
explicitly pass the 'self' argument, Python will do it for you, so
just call :

tmpVal = self.HandleInput("1")


Hope this helps.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top