What are the differences between Ruby's blocks and Python's lambdas?

Newsgroups: comp.lang.python,comp.lang.ruby

···

----- Original Message -----
From: “Greg Ewing (using news.cis.dfn.de)” me@privacy.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, March 26, 2003 9:51 PM
Subject: Re: What are the differences between Ruby’s blocks and Python’s
lambdas?

sdieselil wrote:

What are the differences between Ruby’s blocks and Python’s lambdas?

The main one is that a Python lambda can only contain
an expression, whereas a Ruby block can contain arbitrary
statements.

If that restriction were lifted one day (discussions
come up periodically about doing that somehow) there
would remain another difference – in Python, names
cannot be rebound in a lexically enclosing local
scope.

Is this true? That seems to me a giant
difference.

I don’t mean to doubt you – I don’t know
Python – but why hasn’t this come up
before on this list?

Hal

Hal E. Fulton wrote:


Is this true? That seems to me a giant
difference.

I don’t mean to doubt you – I don’t know
Python – but why hasn’t this come up
before on this list?

Note that these are limitations of Python’s lambda keyword. Lambda is
just syntactic sugar for this:

def _something():

blah(_something)

If you don’t like the keyword lambda, or can’t live with its
restrictions, just don’t use it. There is nothing you can do with it
that you can’t do with named functions.

Paul Prescod