Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Newbie question: Strange TypeError (http://www.velocityreviews.com/forums/t319329-newbie-question-strange-typeerror.html)

Tim Isakson 07-07-2003 04:04 AM

Newbie question: Strange TypeError
 
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

Bruce Wolk 07-07-2003 05:23 AM

Re: Newbie question: Strange TypeError
 
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


Adrien Di Mascio 07-07-2003 07:10 AM

Re: Newbie question: Strange TypeError
 

On Mon, Jul 07, 2003 at 04:04:32AM +0000, Tim Isakson wrote:
> 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,

--
Adrien Di Mascio
LOGILAB, Paris (France).
http://www.logilab.com http://www.logilab.fr http://www.logilab.org




All times are GMT. The time now is 06:08 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57