renaming 'references' to functions can give recursive problems

Jeff Shannon jeff at ccvcorp.com
Wed Feb 16 13:19:22 EST 2005


peter wrote:

> Hello, nice solution:
> but it puzzles me :)
> 
> can anyone tell me why
> -----------correct solution----------------
> def fA(input):
>   return input
> 
> def newFA(input, f= fA):
>    return f(input)

This saves a reference to the original function in the new function's 
default argument.

> -------------infinite loop-----------------
> 
> def fA(input):
>   return input
> 
> def newFA(input):
>    return fA(input)

This does not save any reference to the original function; it simply 
does a run-time lookup of the name, and uses whatever object is 
currently bound to that name.  Since you later rebind the name to this 
new function, it's simply calling itself.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list