# a = b = 1 just syntactic sugar?

Michael Chermside mcherm at mcherm.com
Fri Jun 6 15:24:33 CEST 2003

```> >Lambda expressions are syntactic sugar that allow the embedding of
> >what would otherwise be a def statement within some other statement
>
> Not quite, of course, since not everything that can appear in a def
> statement can appear in a lambda.

No, Terry means precisely what he said. Lambdas can do a SUBSET of
what def can do. Specifically, they can't provide a name for the
function, and they can't include statements.

> [lambda 'designed for side-effect free expressions']
>
> >>I would accept this argument, except for the four examples I
> >>mentioned above.  They are all things which have side effects, and
> >>which are not excluded from appearing in lambda expressions.
> >
> >So what?  Many syntactic constructs can be used for more than
> >intended or thought about by the designer.
>
> The point is that the supposed justification for lambda's restrictions
> - that it is supposed not to have side effects - bears no relation to
> how it really behaves.  The extra complexity introduced by having an
> arbitrary and semantically meaningless (IMHO) set of rules about what
> can be used in lambda and what can't doesn't seem worthwhile.

Again, the argument is from the other direction. *IF* you only wanted
lambda for side-effect-free functions, then you would have little
need (with one exception mentioned below) for statements. The fact
that some expressions ALSO have side effects is irrelevent. And
that's not the REAL motivation for allowing only expressions... the
REAL motivation is the fact that Python makes a clear distinction
between statements and expressions. Many statements have places where
arbitrary expressions can occur, and Python allows expressions to
contain other (arbitrarily complex) expressions, but does NOT have
a concept of expressions containing statements. So lambdas (which
are expressions) can't contain statements. There isn't even a clean
way in which it could be done... unlike (for instance) C and Java
where statements are delimited by ";", statements in Python are
delimited by whitespace and bracket-balancing.

> Why then not allow the same things inside lambda as inside def?  At
> least as far as keeping the function body on a single line.

(1) Because expressions don't contain statements, and there's no
obvious (and readable) syntactic way to change that.
(2) Because it isn't needed (if you use lambda for simple
side-effect-free functions).
(3) Because Guido considers "def" *BETTER* than lambda (because it
forces the user to come up with a name, and it's multi-line
layout is more readable). So he wants to encourage "def" whenever
the task being done is even remotely complicated.

> >Extending lambda to embed even simple statements within expressions
> >is problematical:
>
>     lambda x: assert(x > 0)
>     lambda x: print 'value:', x
>     lambda x: y += x    # (without any change to scoping rules)
>

Actually, ONE of those is convincing to me. Even a side-effect-free
expression ought to be able to raise exceptions, and "raise" (and
also its special-case sibling "assert") is a statement. But if the
function is simple enough, then this won't be an issue. If it is
really needed, it's trivial to define a function "assert_()" which
does what is needed. And if that's too complex, you can fall back
on "def".

> all things you might reasonably want to use as lambda expressions (for
> example an assertion that 'every number in this list is positive').
>
> Then again, I am not a Python developer.  Perhaps there is some
> syntactic ambiguity that would stop the above examples being
> parsable.  But it seems unlikely, one of the big advantages of Python
> is is clean grammar (currently marred only by the strange rules about
> lambda-expressions, IMHO).

Actually, I think you're missing a fundamental point about the grammer.
Unlike many other languages, Python makes a very fundamental
distinction between STATEMENTS and EXPRESSIONS. There are a small
number of clearly specified statements defined in the Python grammer,
each is delimited by whitespace (and bracket-balancing), and there
are no rules for making NEW kinds of statements. Expressions are
defined recursively (nearly all kinds of expressions can contain
other arbitrary expressions) and thus very complicated structures
can be built up within an expression. But the distinction is
maintained throughout the language... it is not a "strange rule".

Hope this helps you make sense of it...

-- Michael Chermside

```

More information about the Python-list mailing list