Newbie question: Strange TypeError
Bruce Wolk
xbawolkx at pacbell.net
Mon Jul 7 01:23:09 EDT 2003
Tim Isakson wrote:
> 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")
--
______
Bruce Wolk
Remove the x's in the e-mail address
More information about the Python-list
mailing list