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

Discussion in 'Python' started by Gnarlodious, May 16, 2011.

  1. Gnarlodious

    Gnarlodious Guest

    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
    Gnarlodious, May 16, 2011
    #1
    1. Advertising

  2. Gnarlodious

    harrismh777 Guest

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

    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
    harrismh777, May 16, 2011
    #2
    1. Advertising

  3. Gnarlodious

    Chris Rebert Guest

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

    On Sun, May 15, 2011 at 8:53 PM, 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?


    Please post the *full* exception Traceback.

    Cheers,
    Chris
    Chris Rebert, May 16, 2011
    #3
  4. Gnarlodious

    Ian Kelly Guest

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

    On Sun, May 15, 2011 at 9:53 PM, Gnarlodious <> 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.
    Ian Kelly, May 16, 2011
    #4
  5. Gnarlodious

    Gnarlodious Guest

    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
    Gnarlodious, May 16, 2011
    #5
  6. Re: TypeError: __init__() takes exactly 1 positional argument (2given)

    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
    Steven D'Aprano, May 16, 2011
    #6
  7. Gnarlodious

    Chris Rebert Guest

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

    On Sun, May 15, 2011 at 9:30 PM, Gnarlodious <> 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
    Chris Rebert, May 16, 2011
    #7
  8. Gnarlodious

    Gnarlodious Guest

    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
    Gnarlodious, May 16, 2011
    #8
  9. Gnarlodious

    Ian Kelly Guest

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

    On Sun, May 15, 2011 at 10:30 PM, Gnarlodious <> 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.
    Ian Kelly, May 16, 2011
    #9
  10. Gnarlodious

    Gnarlodious Guest

    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
    Gnarlodious, May 16, 2011
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Croonenberg
    Replies:
    0
    Views:
    329
    Ron Croonenberg
    Dec 30, 2009
  2. Ron Croonenberg
    Replies:
    0
    Views:
    515
    Ron Croonenberg
    Dec 30, 2009
  3. Ron Croonenberg
    Replies:
    0
    Views:
    464
    Ron Croonenberg
    Dec 30, 2009
  4. Terry Reedy
    Replies:
    1
    Views:
    388
    Carl Banks
    May 26, 2010
  5. Jesse McDonnell
    Replies:
    0
    Views:
    565
    Jesse McDonnell
    May 28, 2010
Loading...

Share This Page