Convert list to another form but providing same information

Ben Bacarisse ben.usenet at bsb.me.uk
Mon Mar 21 20:31:50 EDT 2016


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?

To phrase my question in terms of syntax, why is the : in lambda
followed by a test (the top-level production for an expression in the
grammar) but the : in a function definition is followed by a suite?  I
expected them to be both a suite, but presumably something goes horribly
wrong if that were done.  Now I'm wondering what.

-- 
Ben.



More information about the Python-list mailing list