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

G

Gnarlodious

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
 
H

harrismh777

Gnarlodious said:
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
 
C

Chris Rebert

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
 
I

Ian Kelly

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.
 
G

Gnarlodious

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/625083/python-init-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
 
S

Steven D'Aprano

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
 
C

Chris Rebert

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/625083/python-init-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
 
G

Gnarlodious

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
 
I

Ian Kelly

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/wiki/DebuggingTechniques

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/625083/python-init-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.
 
G

Gnarlodious

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
 

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,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top