>> 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.
It's not the lambda thats evil its the need in Python to limit them to a single expression. If we write def f(n): return n-1 + abs(n-1) and f(n-1)*n or 1 its just as obscure. if we write def f(n) if not ((n-1) + abs(n-1)): return f(n-1) * n else return 1 it gets a little clearer. And the only time the if expression is false is when n is zero or 1 so for the last time: def f(n) if n >1: return f(n-1) * n else: return 1 In fact its the factorial function in very strange disguise! If we could write the lambda as f = lambda n: if n>1: return f(n-1) * n else return 1 is it so much more complex? > Officially, they are for creating "anonymous functions"; > usually they only succeed in creating obscure unreadable drek. Unfortunately that's true. But as a concept they are a fundamental part of computing science and its hard to understamnd higher order programming or explore predicate calculus without them They are also of course very useful as shortcuts but thats usually where the cryptic code comes in. Ruby and Smalltalk's block mechanism is probably more readily grokked by those not interested in mathematical purity! > In My Humble Opinion. And in mine, of course :-) > Yuck. I hate reading lambdas. Personally, I think it's buttugly. I think Python's lambdas are seriously underpowered but not totally without merit. They are manby times better than Java's pathetic anonymous inner class mechanism to do the same job! Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor