a = b = 1 just syntactic sugar?

Martin v. Löwis martin at v.loewis.de
Sat Jun 7 19:28:41 CEST 2003


Ed Avis <ed at membled.com> writes:

> I can think of another rule which is just as simple: allow any
> construct which fits on one line.  Simpler to explain, for sure.

What is the output of

  print a,lambda:print b,c

?

> I gave several examples of where it didn't seem to make any sense -
> sys.stdout.write is an expression while print is a statement,
> setdefault is an expression but ordinary setting of dictionary
> elements is a statement, and so on.

The distinction of statement vs. expression is of purely syntactical
nature. Expressions are operators and function invocations; statements
are more complex. Expressions nest "naturally", statements nest by
indentation, or don't nest at all. It just can't work any other
way. 

Your failed attempt to allow statements inside lambdas just
demonstrates this: Since statements don't nest naturally, any attempt
to make them nest will cause syntactical ambiguities.

> You can sit down and start writing Python code - whether it be
> 'print', 'x = 3', 're.match', 'sys.exit' - without worrying about
> whether the line you're typing is really an expression or a statement.
> You just go ahead and type it, and it works.  This is a strength of
> the language.  It's a weakness that the same simplicity is not there
> when you're writing the body of an anonymous function.

This weakness is inherent; there is no easy way to fix that, or else
it would have been fixed long ago.

Regards,
Martin





More information about the Python-list mailing list