Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > TypeError: __init__() takes exactly 1 positional argument (2 given)

Reply
Thread Tools

TypeError: __init__() takes exactly 1 positional argument (2 given)

 
 
Gnarlodious
Guest
Posts: n/a
 
      05-16-2011
Can someone please explain what I am doing wrong?

Calling script:

from Gnomon import GnomonBase
Gnomon=GnomonBase(3)


Called script:

class GnomonBase(object):
def __init__(self, bench):
# do stuff

But all I get is:
TypeError: __init__() takes exactly 1 positional argument (2 given)

I don't understand, I am only sending one variable. What does it think
I am sending two?

This is Python 3.1.3.

-- Gnarlie
 
Reply With Quote
 
 
 
 
harrismh777
Guest
Posts: n/a
 
      05-16-2011
Gnarlodious wrote:
> class GnomonBase(object):
> def __init__(self, bench): <======= (1) (2)
> # do stuff


This only answers the surface question.... I have not taken any time to
see or understand what (if anything) you are doing which might make any
sense... only that the message is complaining about giving __init__()
two parms, because you gave it two parms....

.... I know you're joking, but I don't know why...?






kind regards,
m harris


 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      05-16-2011
On Sun, May 15, 2011 at 8:53 PM, Gnarlodious <(E-Mail Removed)> wrote:
> Can someone please explain what I am doing wrong?
>
> Calling script:
>
> from Gnomon import GnomonBase
> Gnomon=GnomonBase(3)
>
>
> Called script:
>
> class GnomonBase(object):
> * *def __init__(self, bench):
> * * * *# do stuff
>
> But all I get is:
> TypeError: __init__() takes exactly 1 positional argument (2 given)
>
> I don't understand, I am only sending one variable. What does it think
> I am sending two?


Please post the *full* exception Traceback.

Cheers,
Chris
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      05-16-2011
On Sun, May 15, 2011 at 9:53 PM, Gnarlodious <(E-Mail Removed)> wrote:
> class GnomonBase(object):
> * *def __init__(self, bench):
> * * * *# do stuff
>
> But all I get is:
> TypeError: __init__() takes exactly 1 positional argument (2 given)
>
> I don't understand, I am only sending one variable. What does it think
> I am sending two?


Usually this error means that you forgot to include "self" in the
method signature. As a result it receives two arguments (self and
bench) but only has one defined (bench).

The snippet you posted looks correct, though. It might be easier to
help if you posted the actual code. Also the full stack trace might
be helpful.
 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      05-16-2011
I don't have a trace because I am using mod_wsgi under Apache. Maybe
there is a way to debug using mod_wsgi but I haven't been able to
figure out how.

My problem is that in order to run mod_wsgi I had to downgrade to
Python 3.1.3 which may be causing the problem. This website was
running fine in Py3.2.

I did find an explanation that sounds like this is an intentional
deprecation in Python:
<http://stackoverflow.com/questions/6...and-self-what-
do-they-do>
<http://svn.python.org/view?revision=54539&view=revision>

It looks like we are now expected to initialize instance variables
with a setter statement?

-- Gnarlie
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      05-16-2011
On Sun, 15 May 2011 20:53:31 -0700, Gnarlodious wrote:

> Can someone please explain what I am doing wrong?
>
> Calling script:
>
> from Gnomon import GnomonBase
> Gnomon=GnomonBase(3)
>
>
> Called script:
>
> class GnomonBase(object):
> def __init__(self, bench):
> # do stuff
>
> But all I get is:
> TypeError: __init__() takes exactly 1 positional argument (2 given)
>
> I don't understand, I am only sending one variable. What does it think I
> am sending two?


Whenever you call a method, the instance is automatically provided by
Python as an argument (conventionally called "self") to the function.

So, for any arbitrary method, the call:

instance.method(arg)


is converted to:

type(instance).method(instance, arg)

hence two arguments.

My guess is that your GnomonBase __init__ method is *not* what you show
above, but (probablY) one of the following:

def __init__(bench): # oops, forgot self
# do stuff

def __init__(selfbench): # oops, forgot the comma
# do stuff



--
Steven
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      05-16-2011
On Sun, May 15, 2011 at 9:30 PM, Gnarlodious <(E-Mail Removed)> wrote:
> I don't have a trace because I am using mod_wsgi under Apache. Maybe
> there is a way to debug using mod_wsgi but I haven't been able to
> figure out how.
>
> My problem is that in order to run mod_wsgi I had to downgrade to
> Python 3.1.3 which may be causing the problem. This website was
> running fine in Py3.2.
>
> I did find an explanation that sounds like this is an intentional
> deprecation in Python:
> <http://stackoverflow.com/questions/6...and-self-what-
> do-they-do>
> <http://svn.python.org/view?revision=54539&view=revision>
>
> It looks like we are now expected to initialize instance variables
> with a setter statement?


Er, what are you talking about? That's always been the case; it's
nothing new at all.
Perhaps your "# do stuff" from earlier isn't doing the right stuff?
Posting the actual code would help.

Cheers,
Chris
--
http://rebertia.com
 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      05-16-2011
Well, I have a whole lot of scripts where I could say something like
this:

def __init__(self, var1, var2, var3...):

Now suddenly I have to change them all to run in Python 3.1.3?

This is apparently not a bug. And I rebooted still getting the same
behavior.

Can someone explain it?

-- Gnarlie
 
Reply With Quote
 
Ian Kelly
Guest
Posts: n/a
 
      05-16-2011
On Sun, May 15, 2011 at 10:30 PM, Gnarlodious <(E-Mail Removed)> wrote:
> I don't have a trace because I am using mod_wsgi under Apache. Maybe
> there is a way to debug using mod_wsgi but I haven't been able to
> figure out how.


http://code.google.com/p/modwsgi/wik...gingTechniques

> My problem is that in order to run mod_wsgi I had to downgrade to
> Python 3.1.3 which may be causing the problem. This website was
> running fine in Py3.2.
>
> I did find an explanation that sounds like this is an intentional
> deprecation in Python:
> <http://stackoverflow.com/questions/6...and-self-what-
> do-they-do>
> <http://svn.python.org/view?revision=54539&view=revision>


I don't think those are related. If it were an intentional change in
Python from 2007, then you would be seeing the error in both versions.
I don't see how the stackoverflow link has any bearing on the error
at all.

> It looks like we are now expected to initialize instance variables
> with a setter statement?


You mean like this?

x = Foo()
x.y = z

No, there is no such expectation.
 
Reply With Quote
 
Gnarlodious
Guest
Posts: n/a
 
      05-16-2011
Thanks for all the help, this looks like a bug in mod_wsgi. I tried it
interactively under Py3.1.3 and it behaves normally. I'll take this
over to the mod_wsgi group.

-- Gnarlie
http://Gnarlodious.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
argparse: combine current option value with positional argument Peter Otten Python 1 02-01-2011 04:23 PM
Solved: TypeError: startView() takes exactly 1 argument (3 given) Ron Croonenberg Python 0 12-30-2009 05:01 PM
Re: TypeError: startView() takes exactly 1 argument (3 given) Ron Croonenberg Python 0 12-30-2009 03:43 PM
TypeError: startView() takes exactly 1 argument (3 given) Ron Croonenberg Python 0 12-30-2009 02:00 AM
Confused newbie needs help with "__init__() takes exactly 11 arguments (1 given)" googleboy Python 4 08-19-2005 08:29 PM



Advertisments