Getting method from within method?

Bjorn Pettersen bjorn at roguewave.com
Sat Jul 22 19:58:00 EDT 2000


"David M. Cooke" wrote:
> 
> I want to do recursion, like this:
> 
> def factorial(n):
>     if n <= 1: return 1
>     else return n * factorial(n-1)
> 
> However, this depends on the name of the method staying the same. For
> instance, if I did this:
> 
> fact = factorial
> factorial = None
> 
> fact(5) would give me an error since factorial isn't a method anymore.
> It would be nice if there was some way of referring to the method
> inside of the method.
> 
> Is there any way of doing this easily?

Well, it all depends on what you mean by easy... I would suggest you
find a book on your favorite functional programming language and look up
the Y combinator.  Once you've convinced yourself that's not the way to
do it, you won't mind mentioning factorial in it's body anymore <wink>

is-this-considered-helpful'ly y'rs?
-- bjorn




More information about the Python-list mailing list