Self function

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Mon May 4 11:39:40 CEST 2009


Arnaud Delobelle:
> >>> def bindfunc(f):
> ...     def boundf(*args, **kwargs):
> ...         return f(boundf, *args, **kwargs)
> ...     return boundf

> ...>>> @bindfunc
> ... def fac(self, n):
> ...     return 1 if n <= 1 else n * self(n - 1)
> ...>>> fac(5)
> 120

This is cute, now I have two names to take care of.
Thank you to all the people that have answered.
Another possible syntax:

def fact(n):
    return 1 if n <= 1 else n * return(n - 1)

But I guess most people don't see this problem as important&common
enough to justify changing the language.

Bye,
bearophile



More information about the Python-list mailing list