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 6747133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:24:19+00:00 2026-05-26T12:24:19+00:00

Reading the answer to the question, What’s the difference between globals(), locals(), and vars()?

  • 0

Reading the answer to the question, What’s the difference between globals(), locals(), and vars()?, The following explanation was given for how locals() works:

if locals() is called inside a function it constructs a dictionary of the function namespace as of that moment and returns it — any further name assignments are not reflected in the returned dictionary, and any assignments to the dictionary are not reflected in the actual local namespace

In a comment, it was mentioned that

The part “and any assignments to the dictionary are not reflected in the actual local namespace” might be worded a bit to definite.

linking to the following code:

def f():
    exec "pass"
    locals()["x"] = 42
    print(x)
f()

This returns 42 (at least in CPython 2.7). Without the line exec "pass" python throws a NameError because it can’t find 'x'. This fits with the definition that changes to the dictionary returned by locals() inside a function are not reflected in the namespace. Why does adding the line exec "pass" allow these changes to be reflected? Is this somehow tricking python into thinking it has excited the function?

  • 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-26T12:24:20+00:00Added an answer on May 26, 2026 at 12:24 pm

    This answer to another question has a nice explanation of what is going on. Here is the summary:

    The presence of an exec statement causes Python 2 to compile f in a totally non-optimized fashion, so locals are in a dict instead of in an array as they normally would be. (The performance hit can be considerable).

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

Sidebar

Related Questions

I was reading the answer to this question: Haskell: difference between . (dot) and
I have this question after reading the answer here , what's the difference at
I'm asking this question after reading this answer on StackOverflow. The supplied answer works
Thanks in Advance for reading and answer this question. I got button in asp
I've been reading the answer to a similar question , but I'm still a
After reading this question , i saw the answer by Naveen containing a link
I was reading over this question and wondered if the accepted answer might also
After reading the answer to this question , I learned that SFINAE can be
While I was reading the accepted answer of this question , I had the
Reading what is currently the top answer to a recent question on how/whether to

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.