renaming 'references' to functions can give recursive problems

Michael Spencer mahs at telcopartners.com
Wed Feb 16 12:49:15 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)
> 
> fA = newFA
> 
> is correct and:
> -------------infinite loop-----------------
> 
> def fA(input):
>   return input
> 
> def newFA(input):
>    return fA(input)

In newFA, fA is not bound until you call newFA.  By which time you've re-bound 
fA to newFA, causing the recursion.  In the 'correct' solution above, f is bound 
to the original fA function at the time the def fA statement is executed, which 
is what you want.

> 
> fA = newFA
> 
> gives an infinite recursive loop?
> 
> kind regards
> 
> Peter

Regards
Michael




More information about the Python-list mailing list