Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5472 > unrolled thread

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

Started byGnarlodious <gnarlodious@gmail.com>
First post2011-05-15 20:53 -0700
Last post2011-05-16 04:31 +0000
Articles 10 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  TypeError: __init__() takes exactly 1 positional argument (2 given) Gnarlodious <gnarlodious@gmail.com> - 2011-05-15 20:53 -0700
    Re: TypeError: __init__() takes exactly 1 positional argument (2 given) harrismh777 <harrismh777@charter.net> - 2011-05-15 22:59 -0500
    Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Chris Rebert <clp2@rebertia.com> - 2011-05-15 21:10 -0700
    Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-15 22:12 -0600
      Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Gnarlodious <gnarlodious@gmail.com> - 2011-05-15 21:30 -0700
        Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Chris Rebert <clp2@rebertia.com> - 2011-05-15 21:44 -0700
          Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Gnarlodious <gnarlodious@gmail.com> - 2011-05-15 21:53 -0700
        Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-15 22:54 -0600
          Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Gnarlodious <gnarlodious@gmail.com> - 2011-05-15 22:08 -0700
    Re: TypeError: __init__() takes exactly 1 positional argument (2 given) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-05-16 04:31 +0000

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

FromGnarlodious <gnarlodious@gmail.com>
Date2011-05-15 20:53 -0700
SubjectTypeError: __init__() takes exactly 1 positional argument (2 given)
Message-ID<fb474512-8d88-424b-b7ef-01e20d04478e@h36g2000pro.googlegroups.com>
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

[toc] | [next] | [standalone]


#5473

Fromharrismh777 <harrismh777@charter.net>
Date2011-05-15 22:59 -0500
Message-ID<1j1Ap.30146$Vp.4643@newsfe14.iad>
In reply to#5472
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

[toc] | [prev] | [next] | [standalone]


#5476

FromChris Rebert <clp2@rebertia.com>
Date2011-05-15 21:10 -0700
Message-ID<mailman.1617.1305519015.9059.python-list@python.org>
In reply to#5472
On Sun, May 15, 2011 at 8:53 PM, Gnarlodious <gnarlodious@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#5477

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-05-15 22:12 -0600
Message-ID<mailman.1618.1305519199.9059.python-list@python.org>
In reply to#5472
On Sun, May 15, 2011 at 9:53 PM, Gnarlodious <gnarlodious@gmail.com> 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.

[toc] | [prev] | [next] | [standalone]


#5478

FromGnarlodious <gnarlodious@gmail.com>
Date2011-05-15 21:30 -0700
Message-ID<70608e59-3316-44ce-9d98-2b170d497839@z15g2000prn.googlegroups.com>
In reply to#5477
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

[toc] | [prev] | [next] | [standalone]


#5481

FromChris Rebert <clp2@rebertia.com>
Date2011-05-15 21:44 -0700
Message-ID<mailman.1620.1305521071.9059.python-list@python.org>
In reply to#5478
On Sun, May 15, 2011 at 9:30 PM, Gnarlodious <gnarlodious@gmail.com> 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/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
--
http://rebertia.com

[toc] | [prev] | [next] | [standalone]


#5482

FromGnarlodious <gnarlodious@gmail.com>
Date2011-05-15 21:53 -0700
Message-ID<09233fe8-b1ef-4c1c-a7f6-9aa65de48368@z7g2000prh.googlegroups.com>
In reply to#5481
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

[toc] | [prev] | [next] | [standalone]


#5483

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-05-15 22:54 -0600
Message-ID<mailman.1622.1305521675.9059.python-list@python.org>
In reply to#5478
On Sun, May 15, 2011 at 10:30 PM, Gnarlodious <gnarlodious@gmail.com> 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/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.

[toc] | [prev] | [next] | [standalone]


#5484

FromGnarlodious <gnarlodious@gmail.com>
Date2011-05-15 22:08 -0700
Message-ID<42766145-b076-40c1-bc2b-b85c9c2e1edc@35g2000prp.googlegroups.com>
In reply to#5483
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

[toc] | [prev] | [next] | [standalone]


#5479

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-05-16 04:31 +0000
Message-ID<4dd0a883$0$29983$c3e8da3$5496439d@news.astraweb.com>
In reply to#5472
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web