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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:00:52+00:00 2026-05-10T17:00:52+00:00

Please excuse the vague title. If anyone has a suggestion, please let me know!

  • 0

Please excuse the vague title. If anyone has a suggestion, please let me know! Also please retag with more appropriate tags!

The Problem

I want to have an instance of an imported class be able to view things in the scope (globals, locals) of the importer. Since I’m not sure of the exact mechanism at work here, I can describe it much better with snippets than words.

## File 1 def f1():  print 'go f1!'  class C1(object):     def do_eval(self,x):  # maybe this should be do_evil, given what happens         print 'evaling'         eval(x)         eval(x,globals(),locals()) 

Then run this code from an iteractive session, there there will be lots of NameErrors

## interactive class C2(object):     def do_eval(self,x):  # maybe this should be do_evil, given what happens         print 'evaling'         eval(x)         eval(x,globals(),locals())  def f2():     print 'go f2!'  from file1 import C1 import file1  C1().do_eval('file1.f1()') C1().do_eval('f1()') C1().do_eval('f2()')  file1.C1().do_eval('file1.f1()') file1.C1().do_eval('f1()') file1.C1().do_eval('f2()')  C2().do_eval('f2()') C2().do_eval('file1.f1()') C2().do_eval('f1()') 

Is there a common idiom / pattern for this sort of task? Am I barking up the wrong tree entirely?

  • 1 1 Answer
  • 4 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. 2026-05-10T17:00:53+00:00Added an answer on May 10, 2026 at 5:00 pm

    In this example, you can simply hand over functions as objects to the methods in C1:

    >>> class C1(object): >>>    def eval(self, x): >>>        x() >>> >>> def f2(): print 'go f2' >>> c = C1() >>> c.eval(f2) go f2 

    In Python, you can pass functions and classes to other methods and invoke/create them there.

    If you want to actually evaluate a code string, you have to specify the environment, as already mentioned by Thomas.

    Your module from above, slightly changed:

    ## File 1 def f1():  print 'go f1!'  class C1(object):     def do_eval(self, x, e_globals = globals(), e_locals = locals()):         eval(x, e_globals, e_locals) 

    Now, in the interactive interpreter:

    >>> def f2(): >>>    print 'go f2!' >>> from file1 import *    # 1 >>> C1().do_eval('f2()')   # 2 NameError: name 'f2' is not defined  >>> C1().do_eval('f2()', globals(), locals()) #3 go f2! >>> C1().do_eval('f1()', globals(), locals()) #4 go f1! 

    Some annotations

    1. Here, we insert all objects from file1 into this module’s namespace
    2. f2 is not in the namespace of file1, therefore we get a NameError
    3. Now we pass the environment explictly, and the code can be evaluated
    4. f1 is in the namespace of this module, because we imported it

    Edit: Added code sample on how to explicitly pass environment for eval.

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

Sidebar

Related Questions

Please excuse the vagueness of my title, I really don't know how to succinctly
Please excuse (or improve the title) but I have a silly little problem that's
Please excuse the vague question title, but usually I don't do such kind of
Let me explain my problem. Please excuse me for the long question. Here it
Please excuse my lack of knowledge here but could someone let me know how
Disclaimer Please excuse my ignorance, I've written C# for more than 12 years now
I know very little jquery so please excuse me if I don't make much
Please excuse the poor title - not sure how to phrase what I'm trying
Please excuse my newbie Question. I'm tryin to display data from a mysql table
Please excuse my inexperience with Objective-C. I've only been playing with it for a

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.