"peter" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> indeed it does,  so basically everything works except my original
> solution:
>
> def myfunction(a,b):
>    return a+b
>
> def _myfunction(a,b):
>       return myfunction(a,b)
> myfunction = _myfunction
>
> oh well, it was enough to puzzle my tiny brain....

For newbies reading this, the point is that names within functions, 
including global function names, are looked up when and each time the 
function is *called*, not when it is defined.  So even though _myfunction 
does not *look* recursive, since it appears to call some other function, 
the name rebinding after *makes* it recursive.  Conversely, we can make a 
recursive-looking function not recursive.
>>> def f(): return f() # sure looks infinitely recursive
...
>>> g = f
>>> f = int # arbitrary choice of function that will accept no args
>>> f()
0
>>> g()
0

Moral: be careful about rebinding function names, and the order in which 
you do so.

Terry J. Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to