Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Confusion over calling a nested function inside a parent function

Reply
Thread Tools

Confusion over calling a nested function inside a parent function

 
 
Pyenos
Guest
Posts: n/a
 
      12-22-2006
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.


 
Reply With Quote
 
 
 
 
MacDonald
Guest
Posts: n/a
 
      12-22-2006

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.

 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      12-22-2006
Pyenos <(E-Mail Removed)> 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.
 
Reply With Quote
 
Pyenos
Guest
Posts: n/a
 
      12-22-2006
"MacDonald" <(E-Mail Removed)> 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!
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-23-2006
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/

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-23-2006
Pyenos a écrit :
> [code]


You already got the answer - just a pair of stylistic advices:

> class WORK:


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):
...
 
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
Nested Function Confusion Josh Cheek Ruby 1 01-15-2010 09:28 PM
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
Confusion with calling function of a subclass Pyenos Python 3 12-22-2006 08:13 AM
Nested function X vs. nested varX = function Richard A. DeVenezia Javascript 1 09-09-2003 12:11 AM



Advertisments