Self function

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue May 5 20:58:03 EDT 2009


On Tue, 05 May 2009 06:30:01 -0700, Arnaud Delobelle wrote:

> One issue with automatically binding a local variable to the current
> function is with nested functions:
> 
> def foo()
>     def bar():
>        # How do I call foo() from here?

As foo(), just like you would call foo() from outside of foo(). __this__ 
would refer to bar() inside bar().

My proposal isn't meant to solve the general case "how do I call any 
function anywhere without knowing its name?". I don't think there is a 
general solution to that problem (although obviously you can do it in 
special cases). My proposal is sugar for a fairly general problem "how 
should a function refer to itself?", not an alternative mechanism for 
referring to functions outside of the usual namespace lookup mechanism.



> One solution would be
> 
> def foo()
>     def bar(foo=__this__):
>         foo()


That would be an option, if the programmer had some reason for wanting to 
do this.
 


-- 
Steven



More information about the Python-list mailing list