[Tutor] Cannot Understand

Anna Ravenscroft annaraven at gmail.com
Fri Mar 10 20:30:52 CET 2006


On 3/10/06, Edgar Antonio Rodriguez Velazco <edgar.antonio.rv at gmail.com>
wrote:
>
> Hi,
> Could you please explain this code?.
>
> f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1
>

This is why lambdas are evil. Officially, they are for creating "anonymous
functions"; usually they only succeed in creating obscure unreadable drek.
In My Humble Opinion.

Okay - start from the middle and work our way through:

n is your variable that you're passing as an argument to this unnamed
function.

say, n were 10.

n-1 is 9
add EITHER:
absolute value of n-1 AND do a recursive call to f on 9 (and then on 8, and
then on 7...), multiplied by n
OR add 1.

if you type it into your interpreter, here's the result:
>>> n = 10
>>> f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1
>>> print f(n)
3628800

Yuck. I hate reading lambdas. Personally, I think it's buttugly.

Anna
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060310/d5a9135c/attachment.htm 


More information about the Tutor mailing list