[Tutor] Calling a function within a function within a class...

Eric Brunson brunson at brunson.com
Sat Nov 10 03:43:20 CET 2007


Trey Keown wrote:
> Hey all...
> I'm creating a module for my program, and I need to call a function.
> Here's how it's set up:
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> class DoStuff:
>     def Thing1(self):
>         def ThingToCall(self):
>             print "It worked!"
>     def Thing2(self):
>         #Call the function "ThingToCall" here.
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
> Thanks for any help. I haven't used classes that much before...
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   

Unless someone knows something very clever, I don't think you can.  One 
of the few reasons to declare a function like that is to make the scope 
local to Thing1() so you can pretty much expressly *not* call it from 
outside that scope.  If you don't have a good reason for declaring the 
function in that manner, you shouldn't, especially if you have a need to 
call externally from enclosing scope.

The obvious question is, why would you do that?  :-)

e.



More information about the Tutor mailing list