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

  • SEARCH
  • Home
  • 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 7440861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:55:03+00:00 2026-05-29T10:55:03+00:00

Looking for good explanation of why this code raises SyntaxError . def echo(x): return

  • 0

Looking for good explanation of why this code raises SyntaxError.

def echo(x):
    return x

def foo(s):
    d = {}
    exec(s, {}, d)
    return dict((x,y) for x,y in d.items())

def bar(s):
    d = {}
    exec(s, {}, d)
    return dict((x, echo(y)) for x,y in d.items()) # comment this to compile

s = 'a=1'
foo(s)

  File "test.py", line 11
    exec(s, {}, d)
SyntaxError: unqualified exec is not allowed in function 'bar' it contains a
             nested function with free variables
  • 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-05-29T10:55:04+00:00Added an answer on May 29, 2026 at 10:55 am

    In Python 2.x, exec statements may not appear inside functions that have local “functions” with free variables. A generator expression implicitly defines some kind of “function” (or more precisely, a code object) for the code that should be executed in every iteration. In foo(), this code only contains references to x and y, which are local names inside the generator expression. In bar(), the code also contains a reference to the free variable echo, which disqualifies bar() for the use of exec.

    Also note that your exec statements are probably supposed to read

    exec s in {}, d
    

    which would turn them into qualified exec statements, making the code valid.

    Note that your code would work in Python 3.x. exec() has been turned into a function and can no longer modify the local variables of the enclosing function, thus making the above restriction on the usage of exec unnecessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a good explanation why one piece of code fails to compile
Im looking for a good explanation of what this term means
I am looking for a good explanation of why one would use sliding expiration
I'm looking for an explanation or good free online resources about the organization of
I'm looking for a good article or two, or just an explanation, on how
I'm looking for good/working/simple to use PHP code for parsing raw email into parts.
So I tried searching SO hoping someone had a good explanation of this, with
I'm looking for a primary source (or a really good explanation) to back up
I am looking for an good explanation, maybe with some examples. In my understanding,
I am looking for a good introduction to Python namespaces. I found this tutorial

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.