a = b = 1 just syntactic sugar?

Ed Avis ed at membled.com
Sat Jun 7 01:33:07 CEST 2003

mwilson at the-wire.com (Mel Wilson) writes:

>>    lambda x: print 'value:', x
>    j = map (lambda x: print 'value:', x, some_list)
>   What is the print statement supposed to print?  And since
>print doesn't return anything (not even None), what happens
>to j?

Well, you can answer this by writing the equivalent code with a named

    def f(x):
      print 'value:', x
    j = map(f, some_list)

Python seems to put in a default return value of None, so j becomes a
list of None elements.  The behaviour of an anonymous function should
be the same as that of a named function.

If you mean that the syntax is problematic, because the commas could
belong to 'print' or to the list, it's a fair point.  And I know that
Python developers are keen to keep the syntax simple (and my complaint
is mostly semantic).  Still, I think that parenthesizing where
necessary would not be unreasonable.

Ed Avis <ed at membled.com>

More information about the Python-list mailing list