Convert list to another form but providing same information
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue Mar 22 00:34:49 EDT 2016
On Tuesday 22 March 2016 11:31, Ben Bacarisse wrote:
> Ian Kelly <ian.g.kelly at gmail.com> writes:
>
>> On Mon, Mar 21, 2016 at 2:03 PM, Ben Bacarisse <ben.usenet at bsb.me.uk>
>> wrote:
>>> For experts here: why can't I write a lambda that has a statement in it
>>> (actually I wanted two: lambda l, i: l[i] += 1; return l)?
>>
>> https://docs.python.org/3/faq/design.html#why-can-t-lambda-expressions-
contain-statements
>
> Thanks. That makes it clear that it's just for syntactic and not
> semantic reasons.
>
> However, the explanation ("because Python’s syntactic framework can't
> handle statements nested inside expressions") seemed, at first, to be
> saying you can't because you can't! But the term "syntactic framework"
> hints that it's not really just an arbitrary choice -- that this is
> something about the way Python is parsed that make this choice
> inevitable. Is it to do with the way that indentation has a syntactic
> role?
Don't ask me for a link, because I'm lazy, but this has been discussed like
a bazillion times in the past, here, and on the Python-Ideas and Python-Dev
mailing lists. No offense intended, but if you want definitive statements
from the core developers, you can google for them :-)
But in a nutshell, there are four reasons for the limitation on Python's
lambda statement:
- The use of indentation makes it hard to insert arbitrary statements into
an expression without confusing the parser.
- Guido has a strict rule that Python's syntax must be parsable using a
relatively simply parser. A LL(1) parser, if I remember correctly. So none
of the fancy tricks and hard-to-understand syntax rules that (say) C++ uses.
- Given those limitations, nobody has been able to come up with syntax that
has broad support.
- And besides, quite a large number of people believe that limiting lambda
to simple, single-expression cases is a feature, not a bug.
Nobody has quite ruled out multi-statement lambda, or code blocks, but after
20+ years of people trying to find syntax which works but isn't hated, and
failing, I think it's probably safe to say it isn't going to happen.
--
Steve
More information about the Python-list
mailing list