0

I'm reading Structure and Interpretation of Computer Programs - famous in the field of computer science.

Encouraged by the functional programming, I tried to code in Python instead of Scheme because I find it easier to use. But then comes the question: I've needed a Lambda function many times, but I can't figure out how to use lambda to write an unnamed function with complicated operation.

Here, I want to write a lambda function with a string variable exp as its only argument and execute the exec(exp). But I get an Error:

>>> t = lambda exp : exec(exp)
File "<stdin>", line 1
t = lambda exp : exec(exp)
                    ^
SyntaxError: invalid syntax

how does it happen? How to cope with it?

I read my Python guide book and searched Google without finding the answer I want. Does it mean that the lambda function in python is only designed to be syntactic sugar?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Edward
  • 9
  • 5
  • 4
    `exec` is not a function in python2, it's a statement. you cannot use statements in `lambda`. Do you really need a `lambda` there? Can you not define a normal function? – SilentGhost Nov 12 '12 at 10:55
  • 1
    It's a bad idea to learn from a non-trivial textbook such as SICP using a different programming language, and at that one you're not fully familiar with. After you solve this issue, you are bound to encounter others, such as the one with [assignments in closures](http://stackoverflow.com/questions/7535857/why-doesnt-this-closure-modify-the-variable-in-the-enclosing-scope) or the one with [closures created from loops](http://stackoverflow.com/questions/233673/lexical-closures-in-python). Python is a great language, but it's not a drop-in replacement for Scheme. – user4815162342 Nov 12 '12 at 11:14

2 Answers2

7

You can't use a statement inside lambda body that's why you get that error, lambda only expects expressions.

But in Python 3 exec is a function and works fine there:

>>> t = lambda x: exec(x)
>>> t("print('hello')")
hello

In Python 2 you can use compile() with eval():

>>> t = lambda x: eval(compile(x, 'None','single'))
>>> strs = "print 'hello'"
>>> t(strs)
hello

help on compile():

compile(...)
    compile(source, filename, mode[, flags[, dont_inherit]]) -> code object

    Compile the source string (a Python module, statement or expression)
    into a code object that can be executed by the exec statement or eval().
    The filename will be used for run-time error messages.
    The mode must be 'exec' to compile a module, 'single' to compile a
    single (interactive) statement, or 'eval' to compile an expression.
    The flags argument, if present, controls which future statements influence
    the compilation of the code.
    The dont_inherit argument, if non-zero, stops the compilation inheriting
    the effects of any future statements in effect in the code calling
    compile; if absent or zero these statements do influence the compilation,
    in addition to any features explicitly specified.
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

lambda statements (not functions) can only be one line, and can only consist of an expression. The expression can contain a function call, e.g. lambda: my_func(). exec, however, is a statement, not a function and as a result cannot be used as part of an expression.

The question of multi-line lambdas has been debated several times within the python community, but the conclusion is that its usually better and lesss error-prone to explicitly define a function if that's what's needed.

aquavitae
  • 17,414
  • 11
  • 63
  • 106