Ahh. It seems so obvious now. :) <br><br>Thanks, Wesley & Kent!<br><br><br><div><span class="gmail_quote">On 7/5/07, <b class="gmail_sendername">wesley chun</b> <<a href="mailto:wescpy@gmail.com">wescpy@gmail.com</a>
> wrote:</span><blockquote class="gmail_quote" style="margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; margin-left: 0.80ex; border-left-color: #cccccc; border-left-width: 1px; border-left-style: solid; padding-left: 1ex">
On 7/5/07, Kent Johnson <<a href="mailto:kent37@tds.net">kent37@tds.net</a>> wrote:<br>> wc yeee wrote:<br>> > Hi. Is there a reason the code below raises a syntax error? It's<br>> > probably something silly on my part, but I can't figure it out:
<br>> ><br>> ><br>> > >>> b = lambda: print('bar')<br>> > File "<stdin>", line 1<br>> > b = lambda: print('bar')<br>> > ^
<br>> > SyntaxError: invalid syntax<br>><br>> The body of a lambda has to be an expression, not a statement. print is<br>> a statement.<br>> ><br>><br>> > This works fine too:<br>> ><br>
> > >>> import sys<br>> > >>> a = lambda: sys.stdout.write('foo\n')<br>> > >>> a()<br>> > foo<br>><br>> Right, that is an expression and it is the workaround for your original
<br>> problem.<br><br><br>one thing to keep in mind when thinking, "is this an expression or a<br>statement?" is that the former will always evaluate to some sort of<br>Python object (numerical calculation, function return value [your case
<br>above], some string you've built, etc.) whereas the latter will not<br>have any kind of intrisic value (print, while, if, class, def... none<br>of these have a "value").<br><br>on a partially-related note, print will be changing to a function in
<br>the next generation of Python... see PEP 3105:<br><br><a href="http://www.python.org/dev/peps/pep-3105/">http://www.python.org/dev/peps/pep-3105/</a><br><br>for a list of some of the other changes coming down the line:
<br><a href="http://www.python.org/dev/peps/pep-3100/">http://www.python.org/dev/peps/pep-3100/</a><br><br>cheers,<br>-- wesley<br>- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<br>"Core Python Programming", Prentice Hall, (c)2007,2001
<br> <a href="http://corepython.com">http://corepython.com</a><br><br>wesley.j.chun :: <a href="http://wescpy-at-gmail.com">wescpy-at-gmail.com</a><br>python training and technical consulting<br>cyberweb.consulting : silicon valley, ca
<br><a href="http://cyberwebconsulting.com">http://cyberwebconsulting.com</a><br></blockquote></div><br>