Confusion over calling a nested function inside a parent function

Discussion in 'Python' started by Pyenos, Dec 22, 2006.

  1. Pyenos

    Pyenos Guest

    Code:
    class WORK:
        def getwork(self):
            def choosetable(self):pass
            choosetable() #TypeError: choosetable() takes exactly 1
                          #argument (0 given)
    
    Calling choosetable() at the above location gives me the error
    described above.
    Pyenos, Dec 22, 2006
    #1
    1. Advertising

  2. Pyenos

    MacDonald Guest

    Pyenos wrote:
    >
    Code:
    > class WORK:
    >     def getwork(self):
    >         def choosetable(self):pass
    >         choosetable() #TypeError: choosetable() takes exactly 1
    >                       #argument (0 given)
    > 
    >
    > Calling choosetable() at the above location gives me the error
    > described above.


    Although choosetable is a memeber of an instance method, it is not one
    itself. It should not have self as it's first formal parameter.
    MacDonald, Dec 22, 2006
    #2
    1. Advertising

  3. Pyenos

    Duncan Booth Guest

    Pyenos <> wrote:

    >
    Code:
    > class WORK:
    >     def getwork(self):
    >         def choosetable(self):pass
    >         choosetable() #TypeError: choosetable() takes exactly 1
    >                       #argument (0 given)
    > 
    >
    > Calling choosetable() at the above location gives me the error
    > described above.
    >
    >

    A function defined inside a method is just a function, it's only when a
    function is retrieved from the class dictionary (whether it was defined
    there or assigned to the class later) that it becomes a method.

    Just leave the self parameter off the choosetable function: you can still
    access self from the outer scope of getwork.
    Duncan Booth, Dec 22, 2006
    #3
  4. Pyenos

    Pyenos Guest

    "MacDonald" <> writes:

    > Pyenos wrote:
    > >
    Code:
    > > class WORK:
    > >     def getwork(self):
    > >         def choosetable(self):pass
    > >         choosetable() #TypeError: choosetable() takes exactly 1
    > >                       #argument (0 given)
    > > 
    > >
    > > Calling choosetable() at the above location gives me the error
    > > described above.

    >
    > Although choosetable is a memeber of an instance method, it is not one
    > itself. It should not have self as it's first formal parameter.


    Code:
    class WORK:
            def getwork(self):
                    def choosetable():pass
                    choosetable()
    
    like this? thank you! ;-)
    Pyenos, Dec 22, 2006
    #4
  5. MacDonald a écrit :
    > Pyenos wrote:
    >
    >>
    Code:
    >>class WORK:
    >>    def getwork(self):
    >>        def choosetable(self):pass
    >>        choosetable() #TypeError: choosetable() takes exactly 1
    >>                      #argument (0 given)
    >>
    >>
    >>Calling choosetable() at the above location gives me the error
    >>described above.

    >
    >
    > Although choosetable is a memeber of an instance method,


    s/is a member of/is defined in/
    Bruno Desthuilliers, Dec 23, 2006
    #5
  6. Pyenos a écrit :
    >
    Code:
    [/color]
    
    You already got the answer - just a pair of stylistic advices:
    [color=blue]
    > class WORK:[/color]
    
    1/ By convention, ALL_UPPER names denote (pseudo) symbolic constants. 
    The convention for class names is CamelCase.
    
    2/ better to use new-style classes.
    
    =>
    class Work(object):
       ...
    Bruno Desthuilliers, Dec 23, 2006
    #6
    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. Pyenos
    Replies:
    3
    Views:
    341
    Pyenos
    Dec 22, 2006
  2. Mark Hobley

    Confusion over main function syntax

    Mark Hobley, Apr 27, 2008, in forum: C Programming
    Replies:
    2
    Views:
    228
    Richard Tobin
    Apr 27, 2008
  3. Sur
    Replies:
    4
    Views:
    176
  4. Josh Cheek

    Nested Function Confusion

    Josh Cheek, Jan 15, 2010, in forum: Ruby
    Replies:
    1
    Views:
    135
    Justin Collins
    Jan 15, 2010
  5. Richard A. DeVenezia

    Nested function X vs. nested varX = function

    Richard A. DeVenezia, Sep 8, 2003, in forum: Javascript
    Replies:
    1
    Views:
    124
    Code Ronin
    Sep 9, 2003
Loading...

Share This Page