![]() |
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 |
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 |
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.