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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:44:49+00:00 2026-05-25T19:44:49+00:00

I have two code which really confused me. def get_context(): __gc = globals() __lc

  • 0

I have two code which really confused me.

def get_context():
    __gc = globals()
    __lc = locals()

    def precompiler(code):
        exec code in __lc

    def compiler(script, scope):
        return compile(script, scope, 'eval')

    def executor(expr):
        return eval(expr, __gc, __lc)

    return precompiler, compiler, executor

maker1, compiler1, executor1 = get_context()
maker2, compiler2, executor2 = get_context()

maker1("abc = 123")
maker2("abc = 345")
expr1 = compiler1("abc == 123", "test.py")
print "executor1(abc == 123):", executor1(expr1)
print "executor2(abc == 123):", executor2(expr1)

the result is:
executor1(abc == 123): True
executor2(abc == 123): False

Why the compile execute in the closure only once and the byte-code could run in both?

And there is another code here:

def get_context():
    __gc = globals()
    __lc = locals()

    test_var = 123

    def compiler(script, scope):
        return compile(script, scope, 'eval')

    def executor(expr):
        return eval(expr, __gc, __lc)

    return compiler, executor


compiler1, executor1 = get_context()
compiler2, executor2 = get_context()

expr1 = compiler1("test_var == 123", "test.py")
print "executor1(test_var == 123):", executor1(expr1)
print "executor2(test_var == 123):", executor2(expr1)

the result is:
NameError: name ‘test_var’ is not defined

And how did this happen?

Why does the compile need to check the environment(variable or some others) of the closure while it is not dependent on the closure? This is what I confused!

  • 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-25T19:44:50+00:00Added an answer on May 25, 2026 at 7:44 pm

    In your first example, you are executing ‘abc=123’ in your first context, and ‘abc=345’ in your second context. So ‘test_var==123’ is true in your first context and false in your second context.

    In your second example, you have caught an interesting situation where the interpreter has removed test_var from the context because test_var isn’t referenced.

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

Sidebar

Related Questions

I have two controllers which share most of their code (but must be, nonetheless,
Hi I have the following code which is really long of course, how can
I have these two pieces of code, wich one is more readable? foreach decimal
I have two code bases of an application. I need to copy all the
Hi guys I wrote this code and i have two errors. Invalid rank specifier:
Suppose I have two branches of a project IMClient-MacOS and IMClient-Windows, and their code
I have seen code around with these two styles , I am not not
I have the following code that creates two objects (ProfileManager and EmployerManager) where the
I have some code that will be accessed from two threads: class Timer{ public:
I have some Java code that uses curly braces in two ways // Curly

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.