Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8813397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:50:24+00:00 2026-06-14T03:50:24+00:00

I’m reading Structure and Interpretation of Computer Programs – famous in the field of

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:50:26+00:00Added an answer on June 14, 2026 at 3:50 am

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.