[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Here are a few points about python's lambdas that I haven't seen brought
up yet. This is not meant to trash python, which I like a lot and use all
Firstly, python's indentation rules (which I generally like) make life
difficult when using lambdas. Writing multi-line lambda expressions is
extremely nasty, if not outright impossible. This certainly limits the
uses to which lambdas can be put. OTOH you can always write a named
function if you want to, but that seems quite arbitrary.
Second, if you have lambda you don't need reduce/map/filter because they
can be trivially implemented in terms of lambdas. There may be good
efficiency reasons for hard-coding them into the language, but lambda is
certainly the more fundamental construct. OTOH I'm not sure how easy it
would be to implement list comprehensions in terms of lambdas.
Third, some people really like to use the functional style in python. I
use reduce() quite frequently, and I've noticed that many of the people to
whom I've taught python seem to like it as well. Many people get their
first exposure to FP from python, and like it.
Fourth, it was python that taught me why scheme requires variables to be
explicitly defined before use. If you have this:
(define bar 1)
(lambda (x) (set! bar 2) x))
then you know that the 'bar' inside the lambda must refer to the 'bar' that
is explicitly defined in the outer scope as opposed to introducing a new
local variable. Python doesn't have this, because variables spring into
existence when assigned. This makes the rules for lambdas more complicated
than they would otherwise be. AFAIK common lisp has the same issue, but I
imagine there's a workaround.