469,648 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

Confusion over calling a nested function inside a parent function

Expand|Select|Wrap|Line Numbers
  1. class WORK:
  2. def getwork(self):
  3. def choosetable(self):pass
  4. choosetable() #TypeError: choosetable() takes exactly 1
  5. #argument (0 given)
  6.  
Calling choosetable() at the above location gives me the error
described above.
Dec 22 '06 #1
5 1043

Pyenos wrote:
Expand|Select|Wrap|Line Numbers
  1. class WORK:
  2.     def getwork(self):
  3.         def choosetable(self):pass
  4.         choosetable() #TypeError: choosetable() takes exactly 1
  5.                       #argument (0 given)
  6.  

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.

Dec 22 '06 #2
Pyenos <py****@pyenos.orgwrote:
Expand|Select|Wrap|Line Numbers
  1. class WORK:
  2.     def getwork(self):
  3.         def choosetable(self):pass
  4.         choosetable() #TypeError: choosetable() takes exactly 1
  5.                       #argument (0 given)
  6.  

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.
Dec 22 '06 #3
"MacDonald" <cr*******@gmail.comwrites:
Pyenos wrote:
Expand|Select|Wrap|Line Numbers
  1.  class WORK:
  2.      def getwork(self):
  3.          def choosetable(self):pass
  4.          choosetable() #TypeError: choosetable() takes exactly 1
  5.                        #argument (0 given)
  6.  
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.
Expand|Select|Wrap|Line Numbers
  1. class WORK:
  2. def getwork(self):
  3. def choosetable():pass
  4. choosetable()
  5.  
like this? thank you! ;-)
Dec 22 '06 #4
MacDonald a écrit :
Pyenos wrote:
>>
Expand|Select|Wrap|Line Numbers
  1. class WORK:
  2.    def getwork(self):
  3.        def choosetable(self):pass
  4.        choosetable() #TypeError: choosetable() takes exactly 1
  5.                      #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/

Dec 23 '06 #5
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):
...
Dec 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Andy Baker | last post: by
8 posts views Thread by Z D | last post: by
reply views Thread by hammad.awan_nospam | last post: by
6 posts views Thread by Marc Hoeijmans | last post: by
37 posts views Thread by Tim N. van der Leeuw | last post: by
12 posts views Thread by Peter Cranz | last post: by
16 posts views Thread by Alan Jones | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.